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
依赖于其他对象作为下拉列表中应显示内容的过滤器。有没有一种方法可以干净利落地做到这一点