Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将可自定义查询集用作表单中的下拉选项?_Python_Python 3.x_Django_Django Forms - Fatal编程技术网

Python 如何将可自定义查询集用作表单中的下拉选项?

Python 如何将可自定义查询集用作表单中的下拉选项?,python,python-3.x,django,django-forms,Python,Python 3.x,Django,Django Forms,理想情况下是这样的: forms.py class SelectCar(Form): def __init__(self, custom_queryset): choices = custom_queryset self.fields['choices'] = forms.ModelMultipleChoiceField(choices=choices) def index(request): queryset = Car.objects.fil

理想情况下是这样的:

forms.py

class SelectCar(Form):
    def __init__(self, custom_queryset):
        choices = custom_queryset
        self.fields['choices'] = forms.ModelMultipleChoiceField(choices=choices)
def index(request):
    queryset = Car.objects.filter(brand=brand).all()
    form = SelectCar(queryset)
    context['form'] = form
    return render(request, 'form.html', context)
views.py

class SelectCar(Form):
    def __init__(self, custom_queryset):
        choices = custom_queryset
        self.fields['choices'] = forms.ModelMultipleChoiceField(choices=choices)
def index(request):
    queryset = Car.objects.filter(brand=brand).all()
    form = SelectCar(queryset)
    context['form'] = form
    return render(request, 'form.html', context)
此处的
queryset
依赖于其他对象作为下拉列表中应显示内容的过滤器。有没有一种方法可以干净利落地做到这一点