Предварительное заполнение поля выбора моделью
Я новичок в разработке Django и у меня есть вопрос относительно полей выбора.
Я хочу заполнить поле выбора моделью:
# function.py
[1] ingredients = Ingredients.objects.all()
[2] extras = Extras.objects.filter(pizza_id=pizza_id)
[3] form = SelectIngredientsForm(extras,ingredients)
# forms.py
# Note its Form not ModelForm
class SelectIngredientsForm(forms.Form):
[4] ingredients = forms.ChoiceField(choices=X)
[5] extras = forms.ChoiceField(choices=X)
Мой вопрос таков: Как я могу заполнить варианты выбора в форме ([4] и [5]) объектами, которые я получил ранее [3] от объекта [1] и объекта [2]?
Вместо этого вы можете использовать ModelChoiceField. from functions import ingredients, extras
class SelectIngredientsForm(forms.Form):
ingredients = forms.ModelChoiceField(queryset=ingredients)
extras = forms.ModelChoiceField(queryset=extras)