How to specify Parent-Child relationship within one Model?

喜你入骨 提交于 2019-11-29 23:55:29

问题


For example I have following code:

class FamilyMember(models.Model):
    user = models.OneToOneField(User)

And I have following situations:

a1 = FamilyMember.objects.get(id=1)
a1.first_name = 'John'
a1.last_name = 'Smith'
(a1 is a parent of a2)

a2 = FamilyMember.objects.get(id=2)
a2.first_name = 'Mark'
a2.last_name = 'Smith'
(a2 is a child of a1 and parent of a3 in the same time)

a3 = FamilyMember.objects.get(id=3)
a3.first_name = 'Jason'
a3.last_name = 'Smith'
(a3 is a child of a2)

How can i accomplish that kind of relationships within one model ?


回答1:


You can do this with a simple ForeignKey to self to indicate the parent:

parent = models.ForeignKey('self', blank=True, null=True, related_name='children')

Now you can do a2.parent = a1, and will automatically get access to a1.children.all().




回答2:


You can store the parent FamilyMember in each FamilyMember instance like this:

class FamilyMember(models.Model):
    user = models.OneToOneField(User)
    parent = models.ForeignKey('self', null=True)

This way every FamilyMember have one parent but can be a parent of multiple FamilyMember instances. Also, for simplicity, I've made the parent field nullable.

To save the data you can do:

user_a1 = User(user_a1_data)
user_a1.save()
a1 = FamilyMember(user=user_a1, parent=None)
a1.save()

user_a2 = User(user_a2_data)
user_a2.save()
a2 = FamilyMember(user=user_a2, parent=a1)
a2.save()

The same goes for a3 and etc.

Note user_a1_data and user_a2_data must be the users first_name, last_name and other fields. Did that just not to have to type all fields here.

To retrieve the data you can do:

a2 = FamilyMember.objects.get(pk=1)  # Assuming pk 1 is from a1.
a2_parent = a2.parent  # That'd be a1.
a2_parent.user.first_name # This it a1's name. Don't forget the '.user'.

Note: this is untested example code just to help you get an idea of how you can organize you model relationships. The intention here is not a copy/paste ready solution.

You can adapt this example to store child instead or in addition to parent or to



来源:https://stackoverflow.com/questions/48262544/how-to-specify-parent-child-relationship-within-one-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!