Skip to content

Instantly share code, notes, and snippets.

/one2oneIssue Secret

Created June 8, 2012 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e1651e89eb05e0634511 to your computer and use it in GitHub Desktop.
Save anonymous/e1651e89eb05e0634511 to your computer and use it in GitHub Desktop.
class Relation(MP_Node): # from django-treebeard
#..snip..
peerRel = models.OneToOneField('Relation', editable=False, null=True, default=None)
class Identity(models.Model):
#..snip..
relation = models.OneToOneField('Relation', related_name='identity', editable=False, default=None)
peerIdentity = models.OneToOneField('Identity', editable=False, null=True, default=None)
# myRel is an instance of Relation
# myId is an instance of Identity
# myId.peerIdentity points to another instance of Identity
>>> myRel.identity == myId
True
>>> myId.peerIdentity
<Identity: worksheet: id: 6, title: root 3, self_id: , peer_tag >
# so this shows up
>>> myRel.identity.peerIdentity
>>>
# but why not this?
UPDATE:
>>> myId.peerIdentity
<Identity: worksheet: id: 6, title: root 3, self_id: , peer_tag >
>>> myId.save()
>>> myId.peerIdentity
<Identity: worksheet: id: 6, title: root 3, self_id: , peer_tag >
>>> myId.id
5
>>> myId_n=Identity.objects.get(id=5)
>>> myId_n.peerIdentity
>>>
UPDATE
On the model: Identity and Relation both come in coupled pairs, ie. each instance points to its paired instance, plus an individual ForeignKey.
This is to enable symmetrical access via ForeignKey but still keep asymmetrical information about the Identity/Relation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment