我正在尝试根据用户权限更新选择字段。我有一个布尔字段,如果 False(默认)标准用户可以看到。否则,如果用户拥有我想要显示所有内容的权限。
视图.py
class ExportFormView(FormView):
template_name = 'export.html'
form_class = ExportForm
success_url = '/'
def get_form_kwargs(self):
kwargs = super(ExportFormView, self).get_form_kwargs()
kwargs.update({
'request' : self.request
})
return kwargs
表格.py
class ExportForm(forms.Form):
def __init__(self, request, *args, **kwargs):
self.request = request
super(ExportForm, self).__init__(*args, **kwargs)
choice_list = []
if request.user.has_perms('tracker.print_all'):
e = Muid.objects.values('batch_number').distinct()
else:
e = Muid.objects.values('batch_number').distinct().filter(printed=False)
for item in e:
choice = item['batch_number']
choice_list.append((choice, choice))
batch_number = forms.ChoiceField(choices = choice_list)
我得到的错误:
NameError at /
name 'request' is not defined
任何帮助将不胜感激,我已经坚持了一段时间(并尝试了许多谷歌搜索的 SO 建议/答案。)