Python 使用Django allauth创建自定义字段
我正在尝试为Django allauth注册表单包含自定义字段,但没有取得多大成功。我创建了以下表单和模型: 型号.pyPython 使用Django allauth创建自定义字段,python,django,django-models,django-allauth,Python,Django,Django Models,Django Allauth,我正在尝试为Django allauth注册表单包含自定义字段,但没有取得多大成功。我创建了以下表单和模型: 型号.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='pr
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
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()
AUTH_USER_MODEL = 'user_app.UserProfile'
ACCOUNT_SIGNUP_FORM_CLASS = 'user_app.forms.SignupForm'
forms.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
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()
AUTH_USER_MODEL = 'user_app.UserProfile'
ACCOUNT_SIGNUP_FORM_CLASS = 'user_app.forms.SignupForm'
设置.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
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()
AUTH_USER_MODEL = 'user_app.UserProfile'
ACCOUNT_SIGNUP_FORM_CLASS = 'user_app.forms.SignupForm'
我收到以下错误:
AttributeError:type对象“UserProfile”没有属性“REQUIRED\u FIELDS”
您需要在模型中定义一个名为
REQUIRED\u FIELDS
的元组:
class UserProfile(models.Model):
REQUIRED_FIELDS = ('user',)
user = models.OneToOneField(User, related_name='profile', unique=True)
您可以阅读有关自定义用户的文档,看看Django allauth作者回答的问题: