Home > OS >  How to put the product price with a ManyToManyField between Sellers and Products Table
How to put the product price with a ManyToManyField between Sellers and Products Table

Time:03-17

I want a product price between two table Sellers and Products.

Here are my models-

class Products(models.Model):
    name = models.CharField(max_length=60)
    price= models.IntegerField(default=0)
    category= models.ForeignKey(Category,on_delete=models.CASCADE,default=1 )
    description= models.TextField(blank=True, null= True)
    image= models.ImageField(upload_to='uploads/products/')

    def __str__(self):
        return self.name



class Sellers(models.Model):
    name = models.CharField(max_length=60)
    products = models.ManyToManyField(Products)
    description= models.TextField(blank=True, null= True)
    image= models.ImageField(upload_to='uploads/sellers/')

    def __str__(self):
        return self.name

Where to put the product price for each seller?

CodePudding user response:

I would create a new class Price and store the seller's prices there:

class Price(models.Model):
     seller = models.ForeignKey(Seller,on_delete=models.CASCADE)
     product = models.ForeignKey(Product,on_delete=models.CASCADE)
     price = models.IntegerField()

You should consider to create a uniqueness constraint to avoid multiple product price for one product seller combination

  • Related