secret
anonymous / one2oneIssue
Created

  • Download Gist
one2oneIssue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.