如何创建包含产品和金额的投放模型?

时间:2019-03-02 17:55:38

标签: django django-rest-framework

我正在尝试做某种商店。用户获得带有某些产品(产品模型)的交付(交付模型)。用户可以选择产品的数量。

模型的结构如下:

class Delivery(models.Model):
    some stuff...

class Product(models.Model):
    products = models.ManyToManyField(Product,related_name="deliveries", blank=True, null=True)
    some stuff...

但是现在我真的很困惑如何解决这个看似简单的问题。我在哪里指定每次交付中产品的数量。显然,每次交付的金额会有所不同,因此我几乎无法在产品上指定带有金额的字段……对此我感到很困惑……也许有人可以将我引向有用的资源?

1 个答案:

答案 0 :(得分:1)

我认为这三个模型应该足以让您凝视:

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

class Delivery(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE) # each delivery is for a user 

class Deliverable(models.Model):
    delivery = models.ForeignKey(Delivery, on_delete=models.CASCADE) # each deliverable item belongs to a delivery
    product = models.ForeignKey(Product, on_delete=models.CASCADE) # each deliverable item refers to a product 
    quantity = models.IntegerField() # and how many products are to be delivered