on_delete=models.DO_NOTHING, db_constraint=False
RelatedObjectDoesNotExist
exception which is raised if the local field is empty. An example traceback looks like this (full traceback at the end of this comment):[...]
File "django/db/models/fields/related_descriptors.py", line 177, in __get__ rel_obj = self.get_object(instance) File "django/db/models/fields/related_descriptors.py", line 297, in get_object return super().get_object(instance) File "django/db/models/fields/related_descriptors.py", line 144, in get_object return qs.get(self.field.get_reverse_related_filter(instance)) File "django/db/models/query.py", line 399, in get self.model._meta.object_name app.models.person.DoesNotExist: Person matching query does not exist.
By comparison, if the local field's value is empty (eg None/NULL, no relationship set), then aRelatedObjectDoesNotExist
exception will be raised
instead when attempting to access the relationship. This has the effect of impacting the use ofhasattr()
if following the one-to-one relationships example
for avoiding exception handling. In this case,DoesNotExist
doesn't inherit fromAttributeError
, so it isn't swallowed withhasattr()
butRelatedObjectDoesNotExist
does so it gets swallowed.
RelatedObjectDoesNotExist
. From Simon Charette's comment on the original issue: I'd be in favor of making
ForwardManyToOneDescriptor.get_object
raiseself.RelatedObjectDoesNotExist
as the current code clearly doesn't takedb_constraint=False
into account based on the heuristic comment there.