Python。詹戈。从CreateVi中的get_context_data更改表单提要

2024-09-29 23:26:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我的创建视图有问题。我这样初始化它:

class OutputCreateView(LoginRequiredMixin, generic.CreateView):
    template_name = 'rcapp/common_create_update.html'
    form_class = OutputForm
    model = Output
    def get_context_data(self, **kwargs):
        context = super(OutputCreateView, self).get_context_data(**kwargs)
        # self.form_class.fields['activity_ref'].queryset = Activity.objects.filter(rc_ref=ResultsChain.objects.get(pk=self.kwargs['rc']).pk)
        context['is_authenticated'] = self.request.user.is_authenticated
        return context
    def form_valid(self, form):
        # code code code
        return redirect("/portal/edit/" + str(self.kwargs['rc']) + "/#outputs-table")

我的模型中有一个ForeignKey字段,我想过滤当前视图的选项。在

我的表格是这样设置的:

^{pr2}$

我需要更改activity_ref字段的queryset。 你可以在get_context_data中看到一个注释行,我就是在这里尝试这样做的。但没用。我怎样才能得到我需要的东西?在


Tags: selfformref视图datagetdefcontext
1条回答
网友
1楼 · 发布于 2024-09-29 23:26:03

您需要将choices/queryset传递到表单。在

在OutputCreateView中

def get_form_kwargs(self, *args, **kwargs)
    filter_key = self.kwargs['rc']).pk
    return {'filter_key': key}

像这样,由于意外的参数,它将在创建窗体时给出一个错误。要绕过这个问题并利用它,可以重写init方法。在

在你的输出表格中

^{pr2}$

您不需要设置widgets值,因为它是在init方法中完成的。在

相关问题 更多 >

    热门问题