如何根据值

时间:2016-02-01 14:23:35

标签: django django-models foreign-keys django-smart-selects

我在Django中有以下模型,并使用智能选择:

class Country(models.Model):
    name = models.CharField(max_length=100)

class Province(models.Model):
    name = models.CharField(max_length=100)
    country = models.ForeignKey(Country)

class City(models.Model):
    name = models.CharField(max_length=100)
    country = models.ForeignKey(Country)
    province = models.ForeignKey(Province)

在灯具中,我添加了多个国家,省份和城市。

我在此模型中使用smart-selects进行链接

class WorkArea(models.Model):
    work_area = models.CharField(max_length=100)
    country = models.ForeignKey(Country)
    province =  ChainedForeignKey(Province, chained_field="country",chained_model_field="country")
    city = ChainedForeignKey(City, chained_field=province", chained_model_field="province")

现在我有了这个模型:

class Project(models.Model):
    project_name = models.CharField(max_length=100)
    province = models.ForeignKey(Province)

问题:在模型Project中,我如何仅显示Province模型的省份,country设置为X(如果我有国家"美国&#34和#34;加拿大"我希望字段province仅显示" USA"中的省份列表,以及预选国家/地区)。

1 个答案:

答案 0 :(得分:0)

这是使用limit_choices_to

的解决方案
class Project(models.Model):
    project_name = models.CharField(max_length=100)
    province = models.ForeignKey(Province, limit_choices_to={"country": 1})
相关问题