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

Community
  • 1
  • 1
KingFish
  • 8,773
  • 12
  • 53
  • 81

1 Answers1

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.

Rodney Folz
  • 6,709
  • 2
  • 29
  • 38
  • 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) – KingFish Dec 12 '11 at 08:36
  • There's a [SO question](http://stackoverflow.com/questions/1727564/how-to-create-a-userprofile-form-in-django-with-first-name-last-name-modificati) 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. – Rodney Folz Dec 14 '11 at 08:40
  • 1
    Note that get_profile has been deprecated since django 1.5 and removed in 1.7 – brunch875 Dec 20 '16 at 17:39