Django表单ChoiceField依赖于另一个ChoiceField

时间:2021-02-16 19:21:41

I have the following form:

我有以下表格:

class MyForm(forms.Form):
    country = forms.ChoiceField(
        widget = forms.Select()
    )

    region = forms.CharField(
        widget = forms.Select()
    )

    def update_region(self):
        self['region'].field.widget.choices = get_choices_for_region(
            self['region'].field.initial)

The region choices depend on the country choices. I have an ajax routine to update the region with the selected country. I used a CharField for the region so it will validate properly.

区域选择取决于国家的选择。我有一个ajax例程来更新所选国家/地区的区域。我在该区域使用了CharField,因此它将正确验证。

Actually I prepopulate the form with data inside my view:

实际上,我在视图中预填充了包含数据的表单:

myform['country'].field.initial = 'Switzerland'
myform.update_region()
return ...

Question: Is there a way to force the region choices to update automatically with updating the country?

问题:有没有办法通过更新国家/地区强制区域选择自动更新?

1 个解决方案

#1


0  

I recently happened to face similar issue (and wasted a lot more time on this than would have liked). To make the process fast, I first created Country-Region Lists in the view at the time of page loading. Then, with the help of little Jquery Magic you can dynamically set the fields of region based on the country field chosen.

我最近碰巧遇到了类似的问题(浪费了很多时间,而不是浪费时间)。为了使过程快速,我首先在页面加载时在视图中创建了国家/地区列表。然后,在小Jquery Magic的帮助下,您可以根据所选的国家/地区字段动态设置区域的字段。

Also here is a simple implementation of what you are looking for in this link.

此处还有一个简单的实现,您可以在此链接中查找。

Also there is a even better alternative to make a AJAX call every time the country's field(to get a list of dict of regions for the selected country, let's call it regions_for_country) is changed to update the regions options. Find the following snippet in Jquery that does part of the job.

每当国家的字段(获取所选国家/地区的区域列表,我们称之为regions_for_country)更改为更新区域选项时,还有一个更好的替代方法是进行AJAX调用。在Jquery中找到执行部分工作的以下代码段。

var html = $.map(regions_for_country, function(item){
        return '<option value="' + item['key'] + '">' + item['value'] + '</option>'
    }).join('');

This will generate the html code for the options which u can replace easily by using the .html() command. I would prefer the last method as in the previous method we are wasting time generating the key value pairs of country-region, which can be detrimental for big datasets.

这将为您可以使用.html()命令轻松替换的选项生成html代码。我更喜欢上一种方法中的最后一种方法,我们浪费时间生成country-region的键值对,这对大数据集可能是有害的。

#1


0  

I recently happened to face similar issue (and wasted a lot more time on this than would have liked). To make the process fast, I first created Country-Region Lists in the view at the time of page loading. Then, with the help of little Jquery Magic you can dynamically set the fields of region based on the country field chosen.

我最近碰巧遇到了类似的问题(浪费了很多时间,而不是浪费时间)。为了使过程快速,我首先在页面加载时在视图中创建了国家/地区列表。然后,在小Jquery Magic的帮助下,您可以根据所选的国家/地区字段动态设置区域的字段。

Also here is a simple implementation of what you are looking for in this link.

此处还有一个简单的实现,您可以在此链接中查找。

Also there is a even better alternative to make a AJAX call every time the country's field(to get a list of dict of regions for the selected country, let's call it regions_for_country) is changed to update the regions options. Find the following snippet in Jquery that does part of the job.

每当国家的字段(获取所选国家/地区的区域列表,我们称之为regions_for_country)更改为更新区域选项时,还有一个更好的替代方法是进行AJAX调用。在Jquery中找到执行部分工作的以下代码段。

var html = $.map(regions_for_country, function(item){
        return '<option value="' + item['key'] + '">' + item['value'] + '</option>'
    }).join('');

This will generate the html code for the options which u can replace easily by using the .html() command. I would prefer the last method as in the previous method we are wasting time generating the key value pairs of country-region, which can be detrimental for big datasets.

这将为您可以使用.html()命令轻松替换的选项生成html代码。我更喜欢上一种方法中的最后一种方法,我们浪费时间生成country-region的键值对,这对大数据集可能是有害的。