Accessing a reverse OneToOneField mapping with no object raises DoesNotExist (http://code.djangoproject.com/ticket/10227). I think the difference between when an object is expected and when None is an ordinary result is strong and common enough to warrant a field option.
The vast majority of the time, I want None on a reverse OneToOne mapping, and having to catch DoesNotExist every time is a pain. Occasionally, I want to assume that the object will always exist and raising an exception if it doesn't is correct. contrib.auth.User.get_profile() is an example of a usage that probably actually would want an exception, if it had been implemented with reverse mappings. I'd suggest a reverse_null field option (with a better name, perhaps) for OneToOneField to specify whether the reverse mapping should expect to always have an object or not. This is logically symmetric with null, but not enforced as a database constraint. If False, use the current behavior: raise DoesNotExist. If True, return None, giving behavior consistent with the forward mapping. Backwards compatibility would need a default of False. (This is sort of comparable to http://code.djangoproject.com/ticket/5741: "make queryset get() take a default", but without the backwards compatibility problem.) -- Glenn Maynard --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
