> Let's say I have some models that look like:
>
> class Emotion(models.Model):
> mood = CharField(unique=True)
>
> class Person(models.Model):
> emotions = ManyToManyField(Emotion)
> name = CharField()
>
> angry = Emotion(mood='angry')
> angry.save()
> sad = Emotion(mood='sad')
> sad.save()
> bob = Person(name='Bob')
> bob.emotions.add(angry)
>
> From code I'd like to check if a person has a specific emotion, but if
> that motion hasn't been defined before (IE: the specified mood does
> not exist in the emotion table) I'd like to raise an exception. So
> what I'm looking for is giving the above, a way to check that bob is
> 'angry', bob is not 'sad', and there is no such thing as 'happy'
bob.emotions.filter(mood='angry').count() # 1
bob.emotions.filter(mood='sad').count() # 0
Emotion.objects.get(mood='happy') # raises Emotion.DoesNotExist
--
DR.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment