我正在上Djago在线课程,没有人寻求帮助。希望获得帮助和提示。
我正在学习创建表单(登录/单据)。
到目前为止,我在models.py
from django.db import models
from django.contrib import auth
class User(auth.models.User,auth.models.PermissionsMixin):
def __str__(self):
return "@{}".format(self.username)
在forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['email'].label = 'Email Address'
我想添加其他姓氏作为姓,但是当我在'surname'
(class meta
的{{1}}中添加fields
时出现错误,但是在线培训显示我可以轻松添加其他字段。您能告诉我我做错了什么以及如何添加此字段吗?
当我在forms.py
中添加字段'surname'
时:
forms.py
它给出以下错误:
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','surname','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'
我在raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (surname) specified for User
中
views.py
答案 0 :(得分:0)
首先,您正在使用get_user_model()
方法,但是您正在使用自定义User
模型。
在您的设置中,设置AUTH_USER_MODEL
:
AUTH_USER_MODEL = 'app.User'
然后将surname
字段添加到您的User
模型中:
class User(auth.models.User):
surname = models.Charfield()
def __str__(self):
return "@{}".format(self.username)
运行并应用迁移。然后,您可以将surname
字段添加到表单fields
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','surname','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'
答案 1 :(得分:0)
您需要对forms.py
文件进行以下更改:-
from appname.models import USER
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','surname','password1','password2')
model = USER
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'
答案 2 :(得分:0)
# In your forms.py, you need to add new field 'surname'
For Example:-
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
surname = forms.CharField(max_length=20)
class Meta:
fields = ('username','email','surname','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'