To make an attribute foreign in another table, it has to be made primary in its home table.
Lets take an example of two tables named “primary” and “foreign”.
class primary(models.Model):
name = models.CharField(max_length = 30)
id = models.CharField(max_length=30, primary_key=True)
here in the “primary table” , id should be made primary key to be used as foreign key in secondary table.
class secondary(models.Model):
dob = models.CharField(max_length = 30)
id = models.ForeignKey(primary)
here, the “id” in the table named “secondary” has been made foreign key by using ForeignKey() field with name of the parent table of the attribute “id “ inserted as its parameter to make “id” as the foreign key in the table named “secondary”.
No comments:
Post a Comment