如何将初始选择从视图传递到表单

时间:2019-12-15 09:21:41

标签: python django django-forms django-views

我正在尝试将基于django函数的视图中的选择传递给表单选择字段

我做了什么:

forms.py


class FormName(forms.Form):
    freq = forms.ChoiceField(choices=FREQ)
    start_date = forms.DateField(widget=forms.TextInput(
        attrs={'type': 'date'}
    ), label='Start Date')
    projects = forms.ChoiceField(required=False, label='Project')

views.py

def view_name(request, *args, **kwargs):

        all_projects = list(
            Model.objects.filter(user_id=user_id).values("project_id", "project_name").distinct())

        projects = tuple((q['project_id'], q['project_name']) for q in all_projects)

        form = FormName(initial={'freq': grouping_by, 'start_date': start_date, 'projects': projects})

在视图代码中,我将选择检索为元组的元组,并希望在初始部分将其传递

我该怎么做?

非常感谢

0 个答案:

没有答案