Foreignkeys on multiple fields

22 views
Skip to first unread message

Vikas Rawal

unread,
May 20, 2012, 10:38:24 AM5/20/12
to django...@googlegroups.com
I am new to django, and am trying to build a data-based app.

The models are as follows.

class Intro(models.Model)
Village = models.CharField(max_length=150)
HouseholdNumber = models.IntegerField()
Address = models.CharField(max_length=150)
unique_together = ('Village','HouseholdNumber')


class Members(models.Model)
Village = models.ForeignKey(Intro, to_field='Village',related_name='Village')
HouseholdNumber = models.ForeignKey(Intro, to_field='HouseholdNumber',related_name='HouseholdNumber')
PersonNumber = models.IntegerField()
unique_together = ('Village','HouseholdNumber')

Please note that in the first model, Village and HouseholdNumber are unoque together. Django complains that each of these must be unique? How do I handle this?

Vikas


Daniel Roseman

unread,
May 20, 2012, 11:06:35 AM5/20/12
to django...@googlegroups.com
unique_together needs to be in the inner Meta class.

But your model structure doesn't seem to make sense. Why have two ForeignKeys From Members to Intro? That means that a member could point to different entries for village and household number, which can't be right. The usual thing to do is to have a single FK, and access eg member.intro.village and member.intro.household.
--
DR.

Daniel Roseman

unread,
May 20, 2012, 11:06:40 AM5/20/12
to django...@googlegroups.com

Vikas Rawal

unread,
May 20, 2012, 12:18:28 PM5/20/12
to django...@googlegroups.com


> But your model structure doesn't seem to make sense. Why have two ForeignKeys From Members to Intro? That means that a member could point to different entries for village and household number, which can't be right. The usual thing to do is to have a single FK, and access eg member.intro.village and member.intro.household.


A household is uniquely identified by name of village, and household number. Each person in the second table belongs to one such uniquely identified household.

I am sorry, being new to django, I am unable to explain it in terms used in django.

I would like the second table to automatically inherit name of village and household number from the first table. I thought the right way of doing it would be to define the foreign keys the way I did.

Will be grateful if you could explain what is the right way to achieve this.

Vikas

Message has been deleted

Vikas Rawal

unread,
May 20, 2012, 8:28:27 PM5/20/12
to django...@googlegroups.com



   Forget Django for the moment, and study "database normalization".

   Then consider that Django, so far as I know, adds a primary key
field to each table (model). The main purpose of integer primary key
fields IS to be used as the target of foreign key lookups.

I understand this (at least the basics). I do not particularly like that django introduces the limitation of having a single primary key variable when the database backend does not have such a limitation. I was thinking that I could have django have its integer primary key, but do foreignkey lookups on different variables. I now understand that I was wrong in doing so.




   Tables don't "inherit" from other tables; they "join" to records in
other tables where the record is specified by some singular key. In SQL
terms:

   select people.person-name, address.village, address.household
       from people
       left join address
           on people.addressID = address.ID


This is indeed right. But I would like my second table to have two variables, Village and HouseholdNumber, whose values it directly gets from the first table. I know that I could obtain these through a select query as you have illustrated above. But what I am trying to do is that,when I fill data in an inline formset connected to second table, the table should automatically get these two variables based on the relationship you have used in the query above.

I am sorry for being inaccurate and verbose in my expression, and am grateful for your patience with my ignorance.

Vikas


Reply all
Reply to author
Forward
Message has been deleted
0 new messages