How to edit intermediate table Django?

Viewed 54

So i'm trying to make 2 models like so

class Product(models.Model):
    name = models.CharField(max_length=70)
    availability = models.ManyToManyField(Store)

class Store(models.Model):
    name = models.CharField(max_length=150)
    location = models.CharField(max_length=150)

and the problem is somewhere in database i need to store amount of concrete goods containing in each store. Like Product#1 - Store#1(23), Store#2(12) and etc.

Is there any possibility to store it in intermediate table (from ManyToManyfield) or is there something easier.

1 Answers

You can specify a through=… model [Django-doc]:

class Product(models.Model):
    name = models.CharField(max_length=70)
    availability = models.ManyToManyField(Store, through='StoreProduct')

class Store(models.Model):
    name = models.CharField(max_length=150)
    location = models.CharField(max_length=150)

class StoreProduct(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    store = models.ForeignKey(Store, on_delete=models.CASCADE)
    amount = models.IntegerField(default=1)
Related