简体   繁体   English

Django:可维护的下拉选项

[英]Django: Maintainable drop down choices

I have a newbie problem(sorry for the bad english). 我有一个新手问题(对不起,英语不好)。

I am trying to make a maintainable drop down choices on the address part of my project. 我正在尝试在我的项目的地址部分做出可维护的下拉选项。

Let's say that I have these models: 假设我有以下模型:

class STATES(models.Model):
    state= models.CharField(max_length=255)

class CITIES(models.Model):
    fkey = models.ForeignKey(STATES)
    CITY = models.CharField(max_length=255)

    def __unicode__(self):
        return self.CITY

And forms 和形式

class address(ModelForm):
    state = forms.ModelChoiceField(queryset = STATES.objects.all())
    CITY = forms.ModelChoiceField(queryset = CITIES.objects.all())
class Meta:
    model = REGIONS
    fields = ('state', 'CITY',)

How do i make the form display the cities that is in the state that I selected? 如何使表格显示所选州的城市?

Any pointers, advice, links, or even a sample code would be much appreciated. 任何指针,建议,链接,甚至示例代码都将不胜感激。

Thanks in advance 提前致谢

If you want to create your own, you need ajax to help you. 如果要创建自己的文件,则需要ajax来帮助您。 You need to capture the dropdown change of your state , then using ajax to load the city and display it in the template. 您需要捕捉你的下拉变化state ,然后使用Ajax加载的city ,并在模板中显示它。 There's no shortcut in django to do that. django中没有捷径可以做到这一点。 Otherwise take the third party package like https://github.com/digi604/django-smart-selects , it would simplify this process. 否则,请使用https://github.com/digi604/django-smart-selects之类的第三方程序包,它将简化此过程。

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

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