Document ways of checking for OneToOneField
|Reported by:||EvilDMP||Owned by:||code22|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
https://docs.djangoproject.com/en/dev/topics/db/examples/one_to_one/#one-to-one-relationships points out that if you try to follow the related_name of a OneToOneField to something that doesn't exist, you'll get a DoesNotExist error.
What's missing is advice on how to check if there's a related object.
The most obvious is hasattr. Using the Restaurant/Place example in https://docs.djangoproject.com/en/dev/topics/db/examples/one_to_one/#one-to-one-relationships: hasattr(place, 'restaurant').
It should also be explained a little more why a missing object returns a DoesNotExist, rather than say None.
Change History (9)
comment:1 Changed 20 months ago by timo
- Triage Stage changed from Unreviewed to Accepted
- Type changed from Uncategorized to Cleanup/optimization
comment:2 Changed 20 months ago by code22
- Owner changed from nobody to code22
- Status changed from new to assigned
comment:7 Changed 19 months ago by Tim Graham <timograham@…>
- Resolution set to fixed
- Status changed from assigned to closed