如何防止在DAL依赖选择字段中选择一个选项

huangapple go评论76阅读模式
英文:

How to prevent selecting a choice in DAL dependent choice field

问题

我在我的应用程序中使用了Django Autocomplete Light。在一些视图中,我有具有父子关系的依赖选择(例如Country < Cities)。

为了链接选择项,我使用了DAL文档中规定的"forward"函数,如下所示。

'country': autocomplete.ModelSelect2(url='country-autocomplete',
                                     attrs={'style': 'width:80px', 'data-placeholder': 'Click here to select...',
                                            'data-minimum-input-length': 2}),
'city': autocomplete.ModelSelect2(url='city-autocomplete',
                                  forward=['country'],
                                  attrs={'style': 'width:40px', 'data-placeholder': 'Click here to select...',
                                         'data-minimum-input-length': 2}),

页面按预期工作,但有一个问题,如果在城市选择之前没有选择国家,那么“城市”选择字段会显示所有可用选项(下面是显示情况的图像)。

情况1. 未填写国家选择:

如何防止在DAL依赖选择字段中选择一个选项

在这种情况下,我们可以看到所有包含“lo”的城市选择都会在仅使用“城市”选择字段(而没有先填写“国家”选择字段)时生成。

情况2. 填写国家选择:

如何防止在DAL依赖选择字段中选择一个选项

在第二种情况下,当先选择“国家”选择后再选择城市选择字段时,城市选择字段中生成的名称仅限于国家“United Kingdom”。

我的问题是:

是否有一种方法可以在先从父字段(这里是“国家”)中选择选项之前,阻止从子字段(字段“城市”)中进行选择?如果在DAL中没有内置选项可用,我不反对使用jQuery

英文:

I am using Django Autocomplete Light in my application. In some of the views I have dependent choices with a parent - child relationship (eg. Country < Cities).

For chaining of choices I am using "forward" function as mandated in DAL documentation, as shown below.

    &#39;country&#39;: autocomplete.ModelSelect2(url=&#39;country-autocomplete&#39;,
                                         attrs={&#39;style&#39;: &#39;width:80px&#39;, &#39;data-placeholder&#39;: &#39;Click here to select...&#39;,
                                                &#39;data-minimum-input-length&#39;: 2}),
    &#39;city&#39;: autocomplete.ModelSelect2(url=&#39;city-autocomplete&#39;,
                                      forward=[&#39;country&#39;],
                                      attrs={&#39;style&#39;: &#39;width:40px&#39;, &#39;data-placeholder&#39;: &#39;Click here to select...&#39;,
                                             &#39;data-minimum-input-length&#39;: 2}),

The page is working fine as expected with the caveat that, with no country choice picked up prior to city choices, the "city" choice field shows all available options (following are the images displaying the scenario).

Scenario 1. Country choice not filled:

如何防止在DAL依赖选择字段中选择一个选项

In this instance, we can see all city choices with "lo" in their names are generated when only the "city" choice field is used (without first filling in the "country" choice field).

Scenario 2. Country choice filled:

如何防止在DAL依赖选择字段中选择一个选项

In the second instance, when the city choice field is selected after picking up the "country" choice first, the names generated in the "city" choice field are limited to the country "United Kingdom".

My question is:

Is there a way I may prevent selection from the child field (field "city" here) without first having selected a choice from the parent field (here, the "country")? I am not averse to using jQuery in case there are no in-built options available in DAL.

答案1

得分: 0

仅仅通过在城市自动完成视图中使用一个过滤器:

else:
    qs = qs.filter(country='')

工作就完成了。现在,如果国家选择字段留空,城市选择字段不会生成任何选择项。

英文:

By simply using a filter in the city autocomplete view:

else:
    qs = qs.filter(country=&#39;&#39;)

and the job was done. Now the city choice field is not generating any choices if the country choice field is left blank.

huangapple
  • 本文由 发表于 2020年1月4日 12:20:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定