On Apr 12, 1:51 am, Glenn Maynard <glennfmayn...@gmail.com> wrote:
> 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 tohttp://code.djangoproject.com/ticket/5741:"make 
> queryset get() take a
> default", but without the backwards compatibility problem.)
>
> --
> Glenn Maynard

I agree with this suggestion. For me, checking if
"object.reverserelation is not None" is cleaner and easier to write
than having to try accessing it and catching an exception, especially
in cases where it is uncommon for the related object to actually exist.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to