mood = Emotion.objects.get(mood='thoughtful') The above raises DoesNotExist if the emotion hasn't been defined. If you want to raise a different one, then catch this one and raise your own. Then use mood in your query on the person's emotions.
On Thu, Jul 1, 2010 at 10:31 AM, Doug Warren <doug.war...@gmail.com> wrote: > 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' > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.