[Answer]-How to get the relation between user in my app?

1👍

If you look at it from a DB point-of-view the correct way to do this is to add an ‘id’ field to the UserProfile model, and also add another class like:

class Relations(models.Model):
relation_id = models.IntegerField()
relation_name = models.CharField()

which would be filled with the relations (like: (1, “son of”), (2, “father of”), etc..) (you might also need to make one string for male and one for female. That’s the least interesting point here.

After you’ve created this list of Relations you need to assign the relations to users, and therefore we make another model:

class UserToRelations(models.Model):
user_a_id = models.IntegerField()
user_b_id = models.IntegerField()
relation_id = models.IntegerField()

[You might just do that with the keys, I really don’t remember django that well]

So now you need to populate the UserToRelations table just each time you add a member to the DB. You calculate the relationships there.

The selecting part is where you see the profit of this. Every time a member logs in, you just need to get all the relevant rows from the UserToRelations model and you just present them.

Leave a comment