#12420: "OneToOneField doesn't allow assignment of None"
---------------------------------------------------+------------------------
Reporter: kbrownlees | Owner: andrewsk
Status: closed | Milestone: 1.2
Component: Database layer (models, ORM) | Version: SVN
Resolution: fixed | Keywords:
Stage: Ready for checkin | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by andrewsk):
* status: reopened => closed
* resolution: => fixed
Comment:
@gsakkis - bob.soul from your example is a reverse relation.
Person table doesn't actually have soul_id. So, the way Django deals with
it,
if you assign an instance of Soul (i.e. bobs_soul) to bob.soul, it
modifies bobs_soul.person_id,
so it will be equal to bob.id (line 258). But, then you still need to save
"bobs_soul" object, not
"bob". Which might be a bit counter intuitive, because it looks like we
are trying to modify "bob"
object.
Now in case when we are trying to assign None, to bob.soul, it fails to
modify assigned value, obviously,
because it is not an instance of a Soul.
If our intention is to severe the relation between objects, we can
speculate about two approaches here:
1. Check for the fact that the value is None, and don't do anything.
But in this case assignment of None wouldn't change anything after
saving any of our object to db.
(So we didn't reach our goal)
2. Retrieve previous value of "bobs_soul" and modify it's place_id to be
None.
The problem here is that we will be creating new instance of
"bobs_soul",
then modifying it, but it wouldn't be available outside of the
descriptor. So, there is no way to save this
newly created instance. (Unless we will be in fact modifying object from
related object's cache, and this object
is still referenced by user. Like in your example, you have bobs_soul
created before, so with the second approach
user will be able to do bobs_soul.save() after bob.soul=None, and it
will break the relation)
Both of these approaches doesn't allow us to break the relation between
objects. So, maybe the best way is not to allow assignment
on the reverse relation attribute for OneToOneField, or do not allow
assignment of None, with proper error message, telling that
an assignment should be performed on the other end.
Anyways. The original intention of this ticket was to fix the bug which
didn't allow to assign None to OneToOneFields in model forms.
In my opinion, the issue with reverse relation attribute should be moved
to a separate ticket.
--
Ticket URL: <http://code.djangoproject.com/ticket/12420#comment:14>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.