我试图为特定用户显示一个表单集。
views.py
我有这个:
def get_dep_form(benefit_tab, user):
if benefit_tab == 'medical':
DepMedFormSet = formset_factory(DependentMedicalBenefitForm)
link_formset = DepMedFormSet(user)
else:
return None
return link_formset
forms.py
我有这个:
class DependentMedicalBenefitForm(forms.ModelForm):
has_medical = forms.BooleanField()
med_group_id = forms.CharField(max_length=10)
class Meta:
model = Dependent
fields = [
'has_medical', 'med_group_id'
]
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
queryset = Dependent.objects.filter(employee=self.user)
super(DependentMedicalBenefitForm, self).__init__(*args, **kwargs)
self.fields['has_medical'].queryset = queryset
self.fields['med_group_id'].queryset = queryset
在我的
意见。py公司
def index(request, benefit_tab=None):
#stuff
if benefit_tab:
link_formset = get_dep_form(benefit_tab, request.user)
return render(request, 'benefits/index.html', {
#stuff
'link_formset': link_formset,
})
'User' object has no attribute 'get'
如果我没有传递给用户,并注释掉
初始化