Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best method to store list of foreign keys in a model?

For an example, let's say I'm running an online meat shop. Orders come in "parcels", which contain various types of meat.
My models could look something like this:

class Parcel(models.Model):
    customer_address = models.CharField()
    date_wanted = models.DateField()
    meats = ?
class Meat:
    name = models.CharField()
    cost = models.DecimalField(4, 2)

So, I would have set of Meat objects that represent all the different meats I can put in a parcel. Then, the parcel contains a large amount of meat objects, as well as the address it needs to be shipped to, etc.
How do I store an unknown amount of foreign keys in a Django model?

like image 352
Jaxkr Avatar asked May 14 '13 23:05

Jaxkr


1 Answers

This is a classic many-to-many relationship.

https://docs.djangoproject.com/en/dev/ref/models/fields/#manytomanyfield

like image 123
Peter DeGlopper Avatar answered Oct 23 '22 17:10

Peter DeGlopper