在我正在开发的网站中,我有一个表单来注册扩展userCreationForm
的用户。所述表单仅添加不在默认情况下的字段,但它们位于模型中,例如email,first_name和last_name。
class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email", required=True)
first_name = forms.CharField(label = "First name", required = True )
last_name = forms.CharField(label = "Last name", required = True)
class Meta:
model = User
fields = ("username", "first_name", "last_name", "email", "password1", "password2" )
def __init__(self, *args, **kwargs):
super(RegisterForm, self).__init__(*args, **kwargs)
for fieldname in ['username', 'password1', 'password2']:
self.fields[fieldname].help_text = None
我的问题是,是否有办法添加另一个字段,如图像字段,出生日期等,以便我可以制作更完整的用户个人资料?或者我是否必须创建自己的用户模型?
答案 0 :(得分:1)
向用户模型添加更多字段的Django惯用方法是创建User
为ForeignKey
的第二个模型。所以:
from django.db import models
from django.contrib.auth.models import User
class MyUser(models.Model):
user = models.ForeignKey(User)
# ... other fields. i.e. imagefield, date of birth (dob), etc ..
# example:
dob = models.DateField()