2012-06-28 53 views
95

我在这个表单字段:创建Django的表单字段默认为空查询集

city = forms.ModelChoiceField(label="city", queryset=MyCity.objects.all()) 
district = forms.ModelChoiceField(label="district", queryset=MyDistrict.objects.all()) 
area = forms.ModelChoiceField(label="area", queryset=MyArea.objects.all()) 

区来源于点击城市和地区来自点击区域。用queryset=MyDistrict.objects.all()queryset=MyArea.objects.all()的形式会很重。我如何使查询集默认为空?提前

回答

246

感谢你可以通过这样一个空的查询集:

MyModel.objects.none() 

虽然我不知道你怎么使用这种形式,你可以把作为你的领域的查询集为了得到你所需要的...

你可以找到更多信息here

+2

在这种基于URL参数在你看来你的查询集更改使用情况。然后在您的视图中设置正确的查询集,如下所示: edit_form.fields [“asset”]。queryset = Asset.objects.filter(location_id = location_id) – radtek

相关问题