【问题标题】:Unknown field(s) (username) specified for UserProfile - Django为 UserProfile 指定的未知字段(用户名) - Django
【发布时间】:2016-04-01 10:45:18
【问题描述】:

我正在尝试创建一个注册表单,用于注册User 和他的UserProfile。问题是Djangousername 不可用。

django.core.exceptions.FieldError: Unknown field(s) (username) specified for Use
rProfile

我想形成包含我想要的所有属性(来自内置的UserUserProfile)。

怎么办?我知道问题是因为我添加了UserCreationForm.Meta.fields,但是如何让它工作呢?这似乎是一个清晰而简单的解决方案。

这是表格:

class UserProfileCreationForm(UserCreationForm):
    password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
    password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)

    class Meta(UserCreationForm.Meta):
        model = UserProfile
        fields = UserCreationForm.Meta.fields + ('date_of_birth','telephone','IBAN',)

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            msg = "Passwords don't match"
            raise forms.ValidationError("Password mismatch")
        return password2

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user

用户配置文件模块:

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='user_data')
    date_of_birth = models.DateField()
    telephone = models.CharField(max_length=40)
    IBAN = models.CharField(max_length=40)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=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, blank=True)

    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,
                                                blank=True)

    # TRANSLATOR ATTRIBUTES

    is_translator = models.BooleanField(default=False)

    language_tuples = models.ManyToManyField(LanguageTuple)

    rating = models.IntegerField(default=0)

    number_of_ratings = models.BigIntegerField(default=0)

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

    def __str__(self):
        return '{} {}'.format(self.user.first_name, self.user.last_name)

【问题讨论】:

    标签: python django django-models django-forms


    【解决方案1】:

    正如错误所说,fields 属性只能包含表单所基于的模型中的字段,在本例中为 UserProfile。您不能以这种方式包含用户字段。相反,您需要在类级别手动指定它们,就像您使用密码 1/密码 2 所做的那样。此外,您还需要在 save 方法中对它们进行处理。

    与其做所有这些,不如创建一个包含来自 User 和 UserProfile 的字段的自定义 User 模型更好;这样你就只有一个模型,所有字段都会自动包含在 UserCreationForm 中。

    【讨论】:

    • 谢谢丹尼尔。所以你认为我应该创建一个 CustomUserMode(User,UserProfile): pass?身份验证和类似的东西会正常工作吗?
    猜你喜欢
    • 2013-06-16
    • 2018-02-27
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    相关资源
    最近更新 更多