2

I extended my User Model as described in this SO Posting: Extending the User model with custom fields in Django

However, I'm trying to create a User Create form but I get the following:

'Members' object has no attribute 'set_password'

Here is my model form:

class Members(models.Model):
    user = models.OneToOneField(User)

GENDER_CHOICES = ( ... )
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
date_of_birth     = models.DateField()
class Meta:
    db_table='members'

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Members.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

....and my form....

class SignUpForm(UserCreationForm):
    GENDER_CHOICES = ( ... )
    email = forms.EmailField(label='Email address', max_length=75)
    first_name   = forms.CharField(label='First Name')
    last_name    = forms.CharField(label='Last Name')
    gender  = forms.ChoiceField(widget=RadioSelect, choices=GENDER_CHOICES)
    date_of_birth = forms.DateField(initial=datetime.date.today)

    class Meta:
        model = Members
        fields = ('username', 'email','first_name', 'last_name')

I'm new at Django,so thanks in advance

1 Answer 1

2

The method you chose to extend your User model is by creating a UserProfile (which you've called Member). A Member is not a subclass of User, so you can't call User methods (like set_password) on it.

Instead, your SignUpForm's Meta model should still be User, and to get the extended UserProfile, you should call user.get_profile(). For instance, to get a user's gender, you would call user.get_profile().gender.

Read https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users for more information about extending the user profile.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. Do I override the form object's save parameter, do user.get_profile() then save the new userprofile object? (I renamed Member to UserProfile)
There's a SO question that might help you out. That solution has you overriding save(). I've never written anything that had to use a UserProfile (all my projects extend the User class), so I'm afraid I can't help you too much more.
Note that get_profile has been deprecated since django 1.5 and removed in 1.7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.