recursion - Django recursive relationship -
i'm using django 1.5 , struggling guess pretty basic.
i've got following persons model:
class person(models.model): contact_person = models.manytomanyfield(contactperson) first_name = models.charfield(max_length=30) last_name = models.charfield(max_length=30) ...
the goal add partner of person model. don't want partner or person superior other; should equal. in other words, when looking person, his/her partner should appear (if there one). need able add partner once, either @ woman's or @ men's side (i don't want link them 2 times).
i've searched documentation onetoonefields, recursive onetoones don't seem supported, i.e. nameerror ("name 'person' not defined") when try:
partner = models.onetoonefield(person, blank=true, null=true)
can point me in right direction?
try this:
class person(models.model): first_name = models.charfield(max_length=30) last_name = models.charfield(max_length=30) ... partner = models.onetoonefield('self', null=true, blank=true) def save(self, checkpartner = true, *args, **kwargs): super(person, self).save() if self.partner , checkpartner: self.partner.partner = self self.partner.save(checkpartner = false)
- null true on partner field, has enabled because first person saved not have partner.
- checkpartner parameter has been added save() doesn't fall infinite loop
Comments
Post a Comment