tickets: 10227
This data as json
id | created | changetime | last_pulled_from_trac | stage | status | component | type | severity | version | resolution | summary | description | owner | reporter | keywords | easy | has_patch | needs_better_patch | needs_tests | needs_docs | ui_ux |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
10227 | 2009-02-10 17:40:21 | 2020-11-18 16:59:19 | 2022-03-06 03:46:45.866746 | Accepted | new | Database layer (models, ORM) | New feature | Normal | dev | Support a related_default on OneToOne fields | EDIT: scroll to comment 13 to see what this ticket is about. Referencing a {{{OneToOneField}}} with {{{null=True}}} (i.e. it's optional) when there is no value set will return {{{None}}}, as expected. However, referencing the reverse side of the relation does not follow this behavior and raises a {{{DoesNotExist}}} exception. For example, in the following situation where {{{Shop}}}s have optionally a {{{Place}}} (e.g. webshops need not have a physical location): {{{ class Place(models.Model) address = models.CharField(max_length=80) class Shop(models.Model) place = models.OneToOneField(Place, null=True) name = models.CharField(max_length=50) website = models.URLField() }}} This ''does'' work as expected: {{{ >>> s1 = Shop.objects.create(name='Shop', website='shop.com') >>> print s1.place None }}} But this ''doesn't'' work as expected: {{{ >>> p1 = Place.objects.create(address='123 somestr') >>> p1.shop ... [exception stack trace] ... DoesNotExist: Shop matching query does not exist. }}} I would expect this to be {{{None}}} when {{{null}}} is allowed on the {{{OneToOneField}}}. Please correct my if I'm wrong. I have attached a patch to fix this (checking if {{{null}}} is allowed and returning {{{None}}} or raising the exception appropriately), including tests. Unfortunately this is slightly backwards incompatible, since someone may currently rely on the exception being thrown. | nobody | rvdrijst | onetoone related expection null | 0 | 0 | 0 | 0 | 0 | 0 |