我需要讓一個user
創建具有Category
。當用戶進入到create_event頁的事件,他被出示一張dropdown list
Category
instances.I的需要,以確保只有那些由user
創建的Category
應顯示在下拉列表中使用Django表單子類來創建一個下拉列表
我試圖爲此子類Form
子類,我可以在視圖和模板中使用它。
模板create_event:
<h3>select from existing categories</h3>
{{category_choices_form.as_p}}
視圖create_event:
def create_event(request,..):
user_categories = Category.objects.filter(creator=request.user)
form_data = get_form_data(request)
category_choices_form = CategoryChoicesForm(request.user,form_data)# is this correct?
...
def get_form_data(request):
return request.POST if request.method == 'POST' else None
然後,我創建了Form
子
class CategoryChoicesForm(forms.Form):
def __init__(self, categorycreator,*args, **kwargs):
super(CategoryChoicesForm, self).__init__(*args, **kwargs)
self.creator=categorycreator
categoryoption = forms.ModelChoiceField(queryset=Category.objects.filter(creator=self.creator),required=False,label='Category')
然而,行開始categoryoption =
原因錯誤說name 'self' is not defined
有人可以幫助我嗎?