"OneToOneField doesn't allow assignment of None"
|Reported by:||Kieran Brownlees||Owned by:||Andrii Kurinnyi|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||Ryan Nowakowski, mortas.11@…, George Sakkis||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
In http://code.djangoproject.com/browser/django/trunk/django/forms/models.py (R:11874)
line 53 it states:
53 # OneToOneField doesn't allow assignment of None. Guard against that 54 # instead of allowing it and throwing an error. 55 if isinstance(f, models.OneToOneField) and cleaned_data [f.name] is None: 56 continue
Further reading of the function SingleRelatedObjectDescriptor which
seems to implement most of the OTOF functionality actually states:
207 # If null=True, we can assign null here, but otherwise the value needs 208 # to be an instance of the related class. 209 if value is None and self.related.field.null == False:
It seems that when the code in models.py was written it was by someone who simply had null=False set, which meant that the OTOF (correctly) could not have a None value.
I found the issue when trying to set a OTOF as null in an admin form it says "saved successfully" but never changes the value.
Change History (22)
comment:5 Changed 7 years ago by
|Patch needs improvement:||set|
comment:7 Changed 7 years ago by
|Patch needs improvement:||unset|
|Triage Stage:||Accepted → Ready for checkin|