从特定查询集创建 Django 模型外键字段?

时间:2021-01-02 21:41:17

标签: django django-models

好吧,这很难解释。

我有这些模型:

class Supplier(models.Model):
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, null=True, blank=True)
    email = models.CharField(max_length=200, null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

class Product(models.Model):
    description = models.CharField(max_length=30)
    costprice = models.FloatField(null=True, max_length=99, blank=True)
    retailprice = models.FloatField(null=True, max_length=99, blank=True)
    barcode = models.CharField(null=True, max_length=99, unique=True, blank=True)
    image = DefaultStaticImageField(null=True, blank=True,default='images/item_gC0XXrx.png')
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, null=True, blank=True)

class Order(models.Model):
    item = models.ForeignKey(Product, on_delete=models.CASCADE, default='')
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
    item_qty = models.IntegerField(null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

我的最终目标是能够创建从特定供应商处提取商品的“订单”。

现在,我不知道要做到这一点 - 我如何以某种形式创建一个“订单”的实例,从选定的供应商那里获取商品,而不是从其他所有人那里获取所有商品?

1 个答案:

答案 0 :(得分:0)

您正在寻找limit_choices_to