Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign Key Django Model

I'm trying to create 3 models ; Person, Address and Anniversy. The plan is to have one address and one anniversy for each person. But each address and anniversy can have multiple persons.

So far I have the following, but I think the OneToMany(foreign key) relationships maybe the wrong way round. i.e each address can have one person but each person can have multiple addresses.

from django.db import models  class Person(models.Model):     name = models.CharField(max_length=50)     birthday = models.DateField()      def __unicode__(self):         return u'%s' % (self.name)  class Address(models.Model):     person = models.ForeignKey(Person)     address = models.CharField(max_length=150)      def __unicode__(self):         return u'%s' % (self.address)  class Anniversy(models.Model):     person = models.ForeignKey(Person)     anniversy = models.DateField()      def __unicode__(self):         return u'%s' % (self.anniversy) 
like image 573
felix001 Avatar asked Feb 02 '13 15:02

felix001


People also ask

What is ForeignKey in Django models?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases.

Why we use ForeignKey in Django models?

What is ForeignKey in Django? ForeignKey is a Field (which represents a column in a database table), and it's used to create many-to-one relationships within tables. It's a standard practice in relational databases to connect data using ForeignKeys.

What is primary key in Django model?

If you'd like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you've explicitly set Field.primary_key , it won't add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).


2 Answers

You create the relationships the other way around; add foreign keys to the Person type to create a Many-to-One relationship:

class Person(models.Model):     name = models.CharField(max_length=50)     birthday = models.DateField()     anniversary = models.ForeignKey(         Anniversary, on_delete=models.CASCADE)     address = models.ForeignKey(         Address, on_delete=models.CASCADE)  class Address(models.Model):     line1 = models.CharField(max_length=150)     line2 = models.CharField(max_length=150)     postalcode = models.CharField(max_length=10)     city = models.CharField(max_length=150)     country = models.CharField(max_length=150)  class Anniversary(models.Model):     date = models.DateField() 

Any one person can only be connected to one address and one anniversary, but addresses and anniversaries can be referenced from multiple Person entries.

Anniversary and Address objects will be given a reverse, backwards relationship too; by default it'll be called person_set but you can configure a different name if you need to. See Following relationships "backward" in the queries documentation.

like image 152
Martijn Pieters Avatar answered Oct 02 '22 21:10

Martijn Pieters


I would advise, it is slightly better practise to use string model references for ForeignKey relationships if utilising an app based approach to seperation of logical concerns .

So, expanding on Martijn Pieters' answer:

class Person(models.Model):     name = models.CharField(max_length=50)     birthday = models.DateField()     anniversary = models.ForeignKey(         'app_label.Anniversary', on_delete=models.CASCADE)     address = models.ForeignKey(         'app_label.Address', on_delete=models.CASCADE)  class Address(models.Model):     line1 = models.CharField(max_length=150)     line2 = models.CharField(max_length=150)     postalcode = models.CharField(max_length=10)     city = models.CharField(max_length=150)     country = models.CharField(max_length=150)  class Anniversary(models.Model):     date = models.DateField() 
like image 44
Micheal J. Roberts Avatar answered Oct 02 '22 23:10

Micheal J. Roberts