使用Django allauth创建自定义字段

7

我试图为Django allauth SignUp表单添加自定义字段,但没有成功。我创建了以下表单和模型:

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'

我收到了以下错误: AttributeError: type object 'UserProfile' has no attribute 'REQUIRED_FIELDS'
  1. 这是扩展基类的正确方式吗?
  2. 对于个人资料页面,如何加载扩展类而不是用户类,以便显示已登录的用户名?

您可以阅读有关自定义用户的文档:https://docs.djangoproject.com/en/dev/topics/auth/customizing/#auth-custom-user - Anish Shah
请查看Django allauth作者回答的这个问题:https://dev59.com/4Wct5IYBdhLWcg3wKqQd - Flimm
1个回答

5
您需要在您的模型中定义一个名为REQUIRED_FIELDS的元组:
class UserProfile(models.Model):

    REQUIRED_FIELDS = ('user',)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接