扩展用户的注册表引发错误

时间:2016-03-26 11:11:28

标签: python django django-forms django-authentication

我已经扩展了用户以便能够存储更多属性。现在,我正在尝试为此UserProfile创建注册表单。如何创建创建具有所有属性,要求等的UserProfile的表单?并且需要first_namelast_name

这里有一个非常类似的问题没有答案:link

我试过这个但没有成功:

class UserRegistrationForm(UserCreationForm):
    email = forms.EmailField(required = True)
    first_name = forms.CharField(required = False)
    last_name = forms.CharField(required = False)

    class Meta:
        model = UserProfile
        fields = ('username', 'email', 'password1', 'password2')

    def save(self,commit = True):
        user = super(UserRegistrationForm, self).save(commit = False)
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.telephone = self.cleaned_data['telephone']


        if commit:
            user.save()

        return user

出现此错误:

    class UserRegistrationForm(UserCreationForm):
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages\venv\
lib\site-packages\django\forms\models.py", line 257, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (username) specified for Use
rProfile

这是模特和管理的一部分:

MODELS.PY:

    class UserProfile(models.Model):
        user = models.OneToOneField(User,on_delete=models.CASCADE)

    # ATRIBUTY KTORE BUDE MAT KAZDY
    telephone = models.CharField(max_length=40)

    HOW_DO_YOU_KNOW_ABOUT_US_CHOICES = (
            ('coincidence',u'It was coincidence'),
            ('relative_or_friends','From my relatives or friends'),
            )
    how_do_you_know_about_us = models.CharField(max_length=40, choices=HOW_DO_YOU_KNOW_ABOUT_US_CHOICES, null=True)

    MARITAL_STATUS_CHOICES = (
        ('single','Single'),
        ('married','Married'),
        ('separated','Separated'),
        ('divorced','Divorced'),
        ('widowed','Widowed'),
    )
    marital_status = models.CharField(max_length=40, choices=MARITAL_STATUS_CHOICES, null=True)

    def __unicode__(self):
        return '{} {}'.format(self.first_name,self.surname)

def create_profile_user_callback(sender,instance, **kwargs):
    profile, new = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_profile_user_callback, User)

ADMIN.PY:

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'User_Profile'

class UserAdmin(BaseUserAdmin):
    inlines = (UserProfileInline, )

admin.site.unregister(User)
admin.site.register(User,UserAdmin)

2 个答案:

答案 0 :(得分:0)

您的表单不包含属性用户名,但在Meta下方,包含用户名。尝试在表单字段中添加用户名。

答案 1 :(得分:0)

我认为个人资料上一对一的用户模型是一种不错的方法。唯一的问题是如何创建表格。将它们分开。像这样:

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('extra_field1', 'extra_field2')

在视图中,您可以保存用户,获取pk并将其作为外键添加到配置文件。看看Vitors post以获得一些好的线索。还有一点关于使用后保存信号来确保您始终具有配置文件。