Manan Mehta
Manan Mehta

Reputation: 5929

How to model this kind of a relationship in django?

I want to have two models:

class Receipt(models.Model):
    # Bunch of products

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.IntegerField()

I don't want the product to know about receipt but just the receipt to know about the products. How do I build this relationship?

PS: One product can be in multiple receipts.

Upvotes: 0

Views: 62

Answers (2)

dani herrera
dani herrera

Reputation: 51655

If it is just like you say a manyTomanyField is enough.

But if you need to store also the quantity of each ingredient on each receipe then do you need a many2many with attributes relation

Upvotes: 3

matthieu.cham
matthieu.cham

Reputation: 501

One2Many relationship doesn't come out the box with Django. However, I think this answer is what you're after : Django one-to-many field without reversing dependency

Upvotes: 0

Related Questions