﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26633	OneToOneField's non-existence behaviour is inelegant	Stavros Korokithakis	nobody	"Currently, if you have a model A with a nullable OneToOneField `b_field` to model B, and try to do:


{{{
a = A.objects.all()[0]  # Get a random object.
a.b_field

RelatedObjectDoesNotExist: Number has no allocation.
}}}

It is very inelegant to raise an exception in such a frequent use case as this, and doubly so when returning `None` is completely unambiguous. It makes writing elegant functional code very hard, as you have to break out of the string of functions to check whether this raised an exception.

Right now, if you want to check that the object's relation exists, you can do `hasattr(a, ""b_field"")`, which is rather unclean and, from what I understand, only works because hasattr swallows the exception, which is not the case in Python 3.2+.

I propose that the lookup has a try/except block internally and catches the `RelatedObjectDoesNotExist` exception there, returning `None`. I don't believe there will be any negative side-effects from this change, but code that expects an exception to be raised will need to be changed..."	Cleanup/optimization	closed	Database layer (models, ORM)	1.9	Normal	duplicate			Unreviewed	0	0	0	0	0	0
