0
我的創建視圖有問題。我這樣初始化:Python。 Django的。在CreateView中更改來自get_context_data的表單提要
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字段,我想篩選當前視圖的選項。
我的形式設置這樣的:
class OutputForm(forms.ModelForm):
class Meta:
model = Output
fields = ['value', 'activity_ref']
widgets = {
'value': forms.Select(choices=(#Choises here
,), attrs={"onChange":'select_changed()', 'class':'selector'})
}
我需要更改查詢集的activity_ref領域。 您可以在get_context_data中看到註釋行,這是我嘗試這樣做的地方。但它沒有奏效。我怎樣才能得到我需要的東西?
你能再幫我一次嗎?我得到了一個錯誤,'__init __()在super()中有一個意外的關鍵字參數'queryset''.__ init __(* args,** kwargs) –
你在做pop()作爲__init__的第一行嗎? 在調用super().__ init __()之前,您需要刪除所有傳入的額外krrag。 –