On May 13, 2010, at 10:29 AM, TallFurryMan wrote: > Hello Django users, > > Is there a particular reason why using a related OneToOneField raises > DoesNotExist instead of returning None?
Any query you make that is supposed to return one or more instances, that instead cannot find any results, returns a DoesNotExist. > > | class Person( Model ): > | pass > | > | class Pet( Model ): > | owner = OneToOneField( Person ) > | > | # Assuming "joe" exists as a Person > | >>> kitty = joe.pet > | DoesNotExist: Pet matching query does not exist. Your process is wrong. You are assuming "joe" exists. But your syntax, "kitty = joe.pet", assumes that "pet" already exists. You are assigning to "kitty" a reference to the object named by "joe.pet". If "joe.pet" does not exist, well, you get a DoesNotExist. > > SingleRelatedObjectDescriptor is expecting the related model to exist. > OneToOneField being a subclass of ForeignKey, my first attempt was: > > | >>> kitty = joe.pet.get_or_create() > | DoesNotExist: Pet matching query does not exist. This should be: kitty = Pet.objects.get_or_create(user=joe) > > It's the getter of "joe.pet" which fails. Is it that OneToOneField is > not popular enough to receive such an implementation, or is there a > deeper philosophical reason I don't see? No reason, just that your syntax is incorrect. ---Peter Herndon -- 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.