Django rest framework Best way to link and display value.

13 views
Skip to first unread message

chern...@gmail.com

unread,
Jan 5, 2018, 7:23:09 AM1/5/18
to Django users
I have a table called family, you can see the models.py below.

My partner told me that he wanted the relationship to change to this kind
1,Father
2,Mother
3,Husband
4,Wife
5,Son
6,Daughter
7,Aunt
8,Uncle
9,Nephew
10,Niece
11,Others

This is where he will sent me the id (eg:5) and i will save that 5 into relationship field and display Son

My way of thinking is creating a new table call relationship and hardcode each relationship 1 by 1 (all 11 of it) 
Then change the relationship field in Family into this : relationship = models.ForeignKey(Relationship)
But i have a feeling this is a very troublesome way to do it.

Can anyone help me on what way it will be easier ?

This is the model:

class Family(models.Model):
userId = models.ForeignKey(MyUser, related_name='user_family')
relationship = models.CharField(max_length=100)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
gender = models.CharField(max_length=6, blank=True, null=True)

Dylan Reinhold

unread,
Jan 5, 2018, 7:38:58 AM1/5/18
to django...@googlegroups.com
| But i have a feeling this is a very troublesome way to do it.

Why do you have this feeling? Foreign key to another table sounds fine.
If you don't think that list will change much you could do it as a choice. https://docs.djangoproject.com/en/2.0/ref/models/fields/#choices


Dylan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/275c600e-1e9a-4529-8ee1-3c7b4e2d8b60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chern...@gmail.com

unread,
Jan 5, 2018, 7:55:07 AM1/5/18
to Django users
So foreign key is fine. Thank you. I thought that maybe theres a better way than using foreign key.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages