尝试继承form.Models基类时使用UserCreationForm基类键入对象错误

时间:2017-11-11 05:17:42

标签: python django forms inheritance

(这篇文章之前有很多印刷错误,打字很快,坦率地说我很累,所以我没有真正注意到)

我试图继承一个表单类对象,该对象使用forms.ModelForm到另一个继承自UserCreationForm的表单类对象。据我所知,当表单模型被继承时,只有一个Meta取代。但是,我仍然给了它一个机会,因为这是实现我想要达到的业务逻辑的唯一方法。

下面的代码,请仔细查看并建议解决方法:

class AddressMixin(forms.ModelForm):
    class Meta:
        model = Subscriber
        fields = ['address_one', 'address_two', 'city', 'province']
        widgets = {
            'address_one':forms.TextInput(attrs={'class':'form-control'}),
            'address_two':forms.TextInput(attrs={'class':'form-control'}),
            'city': forms.TextInput(attrs={'class':'form-control'}),
            'province': forms.TextInput(attrs={'class':'form-control'}),
    }

class SubscriberForm(AddressMixin,UserCreationForm):
    first_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'class':'form-control'}))
    last_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'class':'form-control'}))
    email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'class':'form-control'}))
    username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
    password1 = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'type':'password'}))
    password2 = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'type':'password'}))

这是我得到的错误,

AttributeError at /subscribe/signup/
type object 'Subscriber' has no attribute 'USERNAME_FIELD'

同样,我理解错误是指订户模型没有用户名作为属性,因为AddressMixin Meta取代了。但是,AddressMixin模型包含一些需要在注册期间提交的业务逻辑方法,因此需要通过相同的表单。

任何建议都会有所帮助,而我一直在努力解决这个问题。

P.S这是错误的完整追溯......

Traceback:

File "/home/ibrokola/Desktop/PersonalProjects/toLaunch/SaaS/CRM/crm/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner

41。 response = get_response(request)

File "/home/ibrokola/Desktop/PersonalProjects/toLaunch/SaaS/CRM/crm/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response

187。 response = self.process_exception_by_middleware(e,request)

File "/home/ibrokola/Desktop/PersonalProjects/toLaunch/SaaS/CRM/crm/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response

185。 response = wrapped_callback(request,* callback_args,** callback_kwargs)

File "/home/ibrokola/Desktop/PersonalProjects/toLaunch/SaaS/CRM/src/subscribers/views.py" in subscriber_new

81。 else:form = SubscriberForm()

File "/home/ibrokola/Desktop/PersonalProjects/toLaunch/SaaS/CRM/crm/lib/python3.6/site-packages/django/contrib/auth/forms.py" in __init__

94。如果self.fields中的self._meta.model.USERNAME_FIELD:

Exception Type: AttributeError at /subscribe/signup/
Exception Value: type object 'Subscriber' has no attribute 'USERNAME_FIELD'

0 个答案:

没有答案