Milano
Milano

Reputation: 18745

Unknown field(s) (username) specified for UserProfile - Django

I'm trying to create a registration form which registeres User and his UserProfile. The problem is that Django says that username is not available.

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

I would like to form contains all attributes I want (both from built-in User and UserProfile).

What to do with that? I know that the problem is because I added UserCreationForm.Meta.fields but how to make it work? This seems to be a clear and simple solution.

This is the form:

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

USERPROFILE MODULE:

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)

Upvotes: 2

Views: 3672

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

As the error says, the fields attribute can only include the fields from the model the form is based on, which in this case is UserProfile. You can't include the User fields that way. Instead you will need to specify them manually at class level as you have done with password1/password2. Also you will need to do something with them in the save method.

Rather than doing all this, you might find it better to create a custom User model that includes the fields from both User and UserProfile; that way you only have one model, and all the fields will be included automatically on the UserCreationForm.

Upvotes: 2

Related Questions