[英]django model field depend on the value of another field
The use case of my application is I will have various fields to fill and among them one is Industry field and another is Segment Field for brand.我的应用程序的用例是我要填写多个字段,其中一个是行业字段,另一个是品牌的细分字段。 The industry field is like category that brand falls into.
行业领域就像品牌所属的类别。 So, if i choose the
industry
as Health Care
for XYZ brand
then the segment
field should show the items like 'Ayurveda', 'Dental Clinics' (all health care related items).因此,如果我选择
industry
作为XYZ brand
的Health Care
,则segment
字段应显示“阿育吠陀”、“牙科诊所”等项目(所有与医疗保健相关的项目)。 Basically, its like sub-category.基本上,它就像子类别。
Here is a sample model这是一个样品 model
class Industry(models.Model):
name = models.CharField(max_length=150, blank=True, null=True)
class Meta:
verbose_name = 'Industry'
verbose_name_plural = 'Industries'
def __str__(self):
return self.name
class Segment(models.Model):
industry = models.ForeignKey(Industry, related_name='segment', on_delete=models.CASCADE)
name = models.CharField(max_length=150, blank=True, null=True)
class Meta:
verbose_name = 'Segment'
verbose_name_plural = 'Segments'
def __str__(self):
return f'{self.industry.name} - {self.name}'
class BusinessModel(models):
industry = models.ForeignKey(Industry, blank=False, null=False, related_name='industry', on_delete=models.CASCADE)
# segements = models.ForeignKey()
total_investment = models.CharField() # will be choice field
This is a simple model and I have not created Segment model as I am not sure how to approach to this problem.这是一个简单的 model,我还没有创建段 model,因为我不确定如何解决这个问题。 I am just curios to know, if for such case, do i have to something special in
models.py
or in the view side.我只是好奇地知道,如果在这种情况下,我是否必须在
models.py
或视图方面做一些特别的事情。 Such type of things get arise during development phase, thus, I want to be clear on problem solving pattern in django.在开发阶段会出现这类事情,因此,我想弄清楚 django 中的问题解决模式。
UPDATE更新
https://www.franchisebazar.com/franchisor-registration here if you choose industry inside Business model section, the segment will be updated accordingly. https://www.franchisebazar.com/franchisor-registration在这里如果您在商业 model 部分选择行业,该部分将相应更新。
You can have a 3 model design like您可以拥有 3 个 model 设计,例如
class Industry(models.Model):
name = models.CharField(max_length=150, blank=True, null=True)
class Segment(models.Model):
name = models.CharField(max_length=150, blank=True, null=True)
class Mapping(models.Model):
industry = models.ForeignKey(Industry)
segment = models.ForeignKey(Segment)
you can use ChainedForeginKey.. Check the below links您可以使用 ChainedForeginKey .. 检查以下链接
customizing admin of django to have dependent select fields 自定义 django 的管理员以具有依赖的 select 字段
https://django-smart-selects.readthedocs.io/en/latest/installation.html https://django-smart-selects.readthedocs.io/en/latest/installation.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.