4

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 ?

inmate_37
  • 1,218
  • 4
  • 19
  • 33

2 Answers2

18

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().

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    Useful answer! Mind you that in the currrent Django version Foreignkey requires 'on_delete' as second positional argument. – bogl Aug 10 '20 at 09:25
  • While doing this, is it possible to self reference while at the same time deactivating some fields in the parent model? – Newton Karanu Jul 20 '22 at 06:16
4

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

Daniel
  • 597
  • 6
  • 15
  • Your code will fail with a NameError - you can't reference a class from within it's own class statement block since the class does not exists yet. – bruno desthuilliers Jan 15 '18 at 12:37
  • My bad, you can change ` parent = models.ForeignKey(FamilyMember, null=True)` for ` parent = models.ForeignKey('self', null=True)` as read on https://stackoverflow.com/a/15285656/1084033 Edited the answer to reflect that. – Daniel Jan 15 '18 at 13:32