尝试过滤 ChoiceField 的内容

发布于 2024-12-16 12:53:31 字数 1036 浏览 4 评论 0原文

(我的英语不好,抱歉)我在按位置创建搜索形式时遇到问题:我目前的 form.py 如下:

from models import City, Zone

class SearchForm1(forms.Form):
   cityf = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="none")
   zonef = forms.ModelChoiceField(queryset=Zone.objects.all(), empty_label="none")

但这显示数据库中存在的所有城市和地区,我正在尝试要做的是,在第一个字段中显示所有城市,在第二个字段中仅显示与所选城市对应的那些区域。

我尝试这样做:

class SearchForm1(forms.Form):
   cityf = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="none")
   zonef = forms.ModelChoiceField(queryset=City.objects.get(
             name_city="cityf").zone_set.all(), empty_label="none")

^但我收到了这个##错误:^

Exception Type: DoesNotExist
Exception Value:

City matching query does not exist.

Exception Location:

我也一直在看这个:http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-django-admin 但最终不需要留下它有人可以帮我吗?

(My english no is good, sorry) I have a problem creating a form of search by location: I have the form.py currently as follows:

from models import City, Zone

class SearchForm1(forms.Form):
   cityf = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="none")
   zonef = forms.ModelChoiceField(queryset=Zone.objects.all(), empty_label="none")

But this is displayed with all cities and areas exist in the db, and I'm trying to make is that in the 1 st field is show all cities and in the 2nd field to display only those areas corresponding to the selected city.

I tried to do so:

class SearchForm1(forms.Form):
   cityf = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="none")
   zonef = forms.ModelChoiceField(queryset=City.objects.get(
             name_city="cityf").zone_set.all(), empty_label="none")

^ But I recive this ##ERROR: ^

Exception Type: DoesNotExist
Exception Value:

City matching query does not exist.

Exception Location:

I've also been looking at this: http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-django-admin But in the end it does not need to leave it someone can help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-12-23 12:53:31

你不能这样做。由于在呈现表单时,尚未选择城市(第一个字段)。

您可以通过两步向导式的形式来完成此操作。第一种形式只有城市,第二种形式只有区域。在第二种形式中,您可以使用第一种形式中选择的城市来过滤区域。

或者,您可以使用 javascript 来根据第一个字段过滤第二个字段。

You can't do this. Since at the time the form gets rendered, the city (the first field) is not selected yet.

You could either do it in a two-step wizard-style form. Where the first form only has the city, then the second form only the zone. In the second form you filter the zones using the city that was selected in the first form.

Alternatively you could to it using javascript where you filter the second field depending on the first field.

柒夜笙歌凉 2024-12-23 12:53:31

您可能想使用 django 智能选择中的分组选择功能。查看重复问题的答案:

https://stackoverflow.com/a/8390892/538471

You probably want to use the feature Grouped Selects in django smart selects. Check out this answer to a duplicate question:

https://stackoverflow.com/a/8390892/538471

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文