7

I'm trying to include custom fields for the Django allauth SignUp form without much success. I've created the following forms and models:

models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class UserProfile(models.Model):

    user = models.OneToOneField(User, related_name='profile', unique=True)

    # The additional attributes we wish to include.
    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)

    def __unicode__(self):
        return self.user.username

forms.py

from django.contrib.auth import get_user_model
from django import forms
from .models import UserProfile

class SignupForm(forms.ModelForm):

    class Meta:
        model = get_user_model()
        fields = ('username', 'password', 'email', 'website', 'picture') 

    def save(self, user): 
        profile.save()
        user.save()

settings.py

AUTH_USER_MODEL = 'user_app.UserProfile'
ACCOUNT_SIGNUP_FORM_CLASS = 'user_app.forms.SignupForm'

I've receiving the following error: AttributeError: type object 'UserProfile' has no attribute 'REQUIRED_FIELDS'

  1. Is this the correct way to extend the base class?
  2. For the profile page, how do I load the extended class instead of the user class, so that I can display the username that is logged in?
zan
  • 594
  • 2
  • 8
  • 20
  • https://docs.djangoproject.com/en/dev/topics/auth/customizing/#auth-custom-user You can read this doc about custom user – Anish Shah Mar 23 '14 at 07:19
  • Have a look at this question answered by Django allauth author: http://stackoverflow.com/q/12303478/247696 – Flimm Mar 09 '17 at 12:37

1 Answers1

5

You need to define a tuple called REQUIRED_FIELDS in your model:

class UserProfile(models.Model):

    REQUIRED_FIELDS = ('user',)

    user = models.OneToOneField(User, related_name='profile', unique=True)
Shoe
  • 74,840
  • 36
  • 166
  • 272