#14043: Incorrect and/or confusing behaviour with nullable OneToOneField
---------------------------------------------------+------------------------
Reporter: theevilgeek | Owner:
Status: new | Milestone: 1.3
Component: Database layer (models, ORM) | Version: SVN
Resolution: | Keywords:
OneToOneField, cascading delete, nullable
Stage: Ready for checkin | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Comment (by ahebert):
Looks like this is solved here. I'm adding a little more information for
anyone who is trying to work around this in a pre-1.3 version of Django.
The following modification fixed a similar problem on my model.
{{{
class Person(models.Model):
age = models.PositiveIntegerField()
def die(self):
self.soul.become_ghost()
# Update self here, so that self.soul is unavailalbe in the object
passed to delete()
# If self is left alone, then self.soul points to a python object
representing a relationship
# that was deleted from the database in the call to become_ghost().
self = Person.objects.get(pk=self.pk)
# OR, the following to work more generally:
# self = self.__class__.objects.get(pk=self.pk)
self.delete()
class Soul(models.Model):
person = models.OneToOneField(Person, null=True)
is_alive = models.BooleanField(default=True)
def become_ghost(self):
self.person = None
self.is_alive = False
self.save()
}}}
I'd suggest it as a documentation change for earlier versions, but it's so
hack-y.
--
Ticket URL: <http://code.djangoproject.com/ticket/14043#comment:8>
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.