Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating custom fields with Django allauth

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?
like image 756
zan Avatar asked Mar 23 '14 03:03

zan


People also ask

How do I override Django-AllAuth templates?

In order to override templates for django-allauth, you need to create accounts/ directory in your templates folder and add html pages with the names of the pages you want to change.


1 Answers

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)
like image 156
Shoe Avatar answered Sep 27 '22 22:09

Shoe