使用Django allauth创建自定义字段

问题描述 投票:4回答:1

我试图为Django allauth SignUp表单包含自定义字段而没有太大成功。我创建了以下表单和模型:

models.朋友

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.朋友

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.朋友

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

我收到以下错误:AttributeError:type object'UserProfile'没有属性'REQUIRED_FIELDS'

  1. 这是扩展基类的正确方法吗?
  2. 对于配置文件页面,如何加载扩展类而不是用户类,以便我可以显示登录的用户名?
python django django-models django-allauth
1个回答
3
投票

您需要在模型中定义一个名为REQUIRED_FIELDS的元组:

class UserProfile(models.Model):

    REQUIRED_FIELDS = ('user',)

    user = models.OneToOneField(User, related_name='profile', unique=True)
© www.soinside.com 2019 - 2024. All rights reserved.