我有一个带有一个额外字段的 django自定义用户模型: MyUser
# models.py
from django.contrib.auth.models import AbstractUser
class MyUser(AbstractUser):
age = models.PositiveIntegerField(_("age"))
# settings.py
AUTH_USER_MODEL = "web.MyUser"
# forms.py
class SignupForm(forms.Form):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
age = forms.IntegerField(max_value=100)
class Meta:
model = MyUser
def save(self, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.age = self.cleaned_data['age']
user.save()
# settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'web.forms.SignupForm'
提交后SignupForm
(属性字段MyUser.age
正确呈现),我收到此错误:
/accounts/signup/ 处的 IntegrityError
(1048,“列‘年龄’不能为空”)
存储自定义用户模型的正确方法是什么?
django-allauth:0.12.0;django:1.5.1;Python 2.7.2