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