Skip to content Skip to sidebar Skip to footer

Inline Editing Of Manytomany Relation In Django

After working through the Django tutorial I'm now trying to build a very simple invoicing application. I want to add several Products to an Invoice, and to specify the quantity of

Solution 1:

You need to change your model structure a bit. As you recognise, the quantity doesn't belong on the Product model - it belongs on the relationship between Product and Invoice.

To do this in Django, you can use a ManyToMany relationship with a through table:

class Product(models.Model):
    ...

class ProductQuantity(models.Model):
    product = models.ForeignKey('Product')
    invoice = models.ForeignKey('Invoice')
    quantity = models.IntegerField()

class Invoice(models.Model):
    ...
    products = models.ManyToManyField(Product, through=ProductQuantity)

Post a Comment for "Inline Editing Of Manytomany Relation In Django"