简体   繁体   English

动态 django 选择字段

[英]Dynamic django choice field

I have 4 models: Products (the list of products: freezers, microwaves, tvs and pcs), ProductType (entertainment and home appliances), Credit (a credit is registered on each purchase) and PurchaseReason (the reason why the customer has bought the product).我有 4 个型号: Products (产品列表:冰柜、微波炉、电视和个人电脑)、 ProductType (娱乐和家用电器)、 Credit (每次购买都注册一个 Credit)和PurchaseReason (客户购买该产品的原因)产品)。

The PurchaseReason depend on the productType , so the purchaseReason has a foreignKey field productType . PurchaseReason依赖于productType ,因此purchaseReason有一个 foreignKey 字段productType

In addition, each credit has a product as foreignKey and a purchaseReason as foreignKey.另外,每个credit都有一个product作为foreignKey和一个purchaseReason作为foreignKey。

Also, I have the ProductReason field as a choice field in the credit model, and I want the options to be set dynamically based on the product field of the credit model.另外,我将ProductReason字段作为credit model 中的选择字段,并且我希望根据信用 model 的product字段动态设置选项。

I'm creating an API so I think this cant be handle with modelForms, but i'm not sure.我正在创建一个 API 所以我认为这不能用 modelForms 处理,但我不确定。 The hard work would be with the serializers (DRF) and with the django-admin (specially this one because in my product the django admin plays an important role)艰苦的工作将是使用序列化程序(DRF)和 django-admin(特别是这个,因为在我的产品中,django 管理员起着重要作用)

What would be the best approach to manage my models in Django?在 Django 中管理我的模型的最佳方法是什么?

Here are my models.这是我的模型。 In credit I'm not sure how to implemente the purchase reason:在信用我不知道如何实施购买原因:

class Credit(models.Model):
    client = models.ForeignKey('clients.Client', on_delete=models.SET_NULL)
    name = models.CharField(max_length=30, null=False, blank=True)
    product = models.ForeignKey('product',on_delete=models.SET_NULL)
    reason_purchase = models.ChoiceField(????)
class PurchaseReason(models.Model):
    product_type = models.ForeignKey(product_type, on_delete=models.CASCADE)
    reason = models.CharField(max_length=30, null=False, blank=True)
class ProductType(models.Model):
    name = models.CharField(max_length=30, null=False, blank=False)
class Product(models.Model):
    model = models.CharField(max_length=30, default=None, null=True)
    product_type = models.ForeignKey(product_type, on_delete=models.CASCADE)

When we use the foreign key, we need to mention the model name of that particular model so that we can integrate that particular model in that model as a reference entity. When we use the foreign key, we need to mention the model name of that particular model so that we can integrate that particular model in that model as a reference entity. Have a look at this example.看看这个例子。

from django.db import models

class Reporter(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)

class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline

    class Meta:
        ordering = ['headline']

you've not mentioned the model name properly.您没有正确提及 model 名称。 it should be Product in place of 'product' in the Credit class, product field.它应该是Product代替Credit class产品字段中的“product”

use this reference https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_one/使用此参考https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_one/

i think you should be able to use the Foreignkey field properly after this.我认为您应该能够在此之后正确使用外键字段。 Although, if you can't, you can share the actual objective.虽然,如果你不能,你可以分享实际目标。 i will help you to write the correct model.我将帮助您编写正确的 model。 Best wishes:)最好的祝愿:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM