[Django]-How does a model relate to another model via Foreignkey?

3👍

Telephone.objects.filter(contact__nickname='jill')

See the documentation on making queries

 what if i wanted to be able to edit jill and give her more than one number via
 different saves is this possible?

Yes it is. To give one thing in contact objects multiple telephone numbers, there are two ways – a One-To-One relationship via a ForeignKey, or a many-to-many relationship. I suggest you read through the models topic in the documentation.

Edit

You can give Jill more than one phone number on each save in the model layout that you have.

Suppose Jill has a new line, so you want to add a new phone number. The way you have it structured now, you would first have to go and find ‘Jill’ in Contacts, and then give her a new phone number:

jill = Contact.objects.get(nickname='jill') # assuming you only have one jill
new_num = Telephone.objects.create(contact=jill,number=5551212)

Now if you want all of Jill’s contacts, you would do:

call_jill = Telephone.objects.filter(contact=jill) # if you already pulled jill
call_jill = Telephone.objects.filter(contact__nickname='jill')

The other way to do it would be with a ManyToMany:

   class Telephone(models.Model):
      number = models.PhoneNumberField()

   class Contact(models.Model):
      # your normal fields
      phones = models.ManyToMany(Telephone)

Now, you can do:

   jills_phones = Contact.objects.get(nickname='jill').phones.all()

To add a new phone:

   jill = Contact.objects.get(nickname='jill')
   jill.phones.add(Telephone.objects.create(number=5551212))
   jill.save()

You can also do the reverse, find out whose phone this is:

   phone = Telephone.objects.get(number=5551212)
   whose_is_it = phone.contact_set.all()

So what’s the difference?

They both sound similar, but the differences are subtle. A simple analogy is easy. One Manufacturer can have many Cars, but one Car can only have one Manufacturer – this is a one-to-one between Car and Manufacturer.

A Pizza can have many toppings, and each topping can be on multiple pizzas. In this case, Pizza has ManyToMany relationship with Toppings.

You might be thinking that I can do that with a ForeignKey. You are right, but consider this scenario:

Suppose you start off and you have no toppings. So you create olives as a topping and then add it to a pizza. Great.

Now you want the same pizza to have green peppers as well. Now you are stuck, because one pizza can only have one topping (due to ForeignKey).

So you end up creating a third model, which holds a reference to a pizza and a topping. So instead of putting toppings on pizzas, you put them on this other model:

class Pizza(models.Model):
   topping = models.ForeignKey('Topping')

class Topping(models.Model):
   name = models.CharField(max_length=100)

class AssembledPizza(models.Model):
   topping = models.ForeignKey('Topping')
   pizza = models.ForeignKey('Pizza')

However, if you did it the correct way with a ManyToMany, you have:

class Pizza(models.Model):
   toppings = models.ManyToMany('Topping')

class Topping(models.Model):
   name = models.CharField(max_length=100)

Then you would do:

Here I am assuming you are a new restaurant and have no toppings. See the documentation for create for more information.

veggie = Pizza()
veggie.toppings.add(Topping.objects.create(name='Olives'))
veggie.toppings.add(Topping.objects.create(name='Peppers'))
veggie.save()

I hope this clarifies it a bit.

Leave a comment