Вы можете использовать атрибут through
отношения ManyToMany
. См. ЗДЕСЬ .
Это может привести к следующему:
class Product(models.Model):
idProduct = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Warehouse(models.Model):
idWareouse = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
products = models.ManyToManyField(Product, through='Catalog')
def __str__(self):
return self.name
class Catalog(models.Model):
products = models.ForeignKey(Product, on_delete=models.CASCADE)
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
quantity = models.IntegerField()
См. Поле products
в классе Warehouse
:
products = models.ManyToManyField(Product, through='Catalog')
В документации приведен полный пример .