Django-UserModel-如何在django/contrib/auth/forms.py中创建自定义文本字段 [英] Django - UserModel - How to create Custom Text Field within the django/contrib/auth/forms.py
问题描述
我的问题-如何在django/contrib/auth/forms.py中创建自定义文本字段.?
My Question - How to create Custom Text Field within the django/contrib/auth/forms.py. ?
正在尝试调整Django默认用户模型.正在添加名称为"bio"的测试字段
Am trying to tweak the Django default User Model . Am adding a test field by the name "bio"
到目前为止,我在/python3.6/site-packages/django/contrib/auth/forms.py
文件中的代码如下-
My code so far in the /python3.6/site-packages/django/contrib/auth/forms.py
file is as below -
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput,
help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
bio = forms.CharField( #FOO_bio # this value --not getting saved in PSQL
label=_("bio_test within Forms.py"),
#widget=forms.PasswordInput, #Didnt work thus switched to forms.TextInput
widget=forms.TextInput,
strip=False,
help_text=_("Enter some dummy BIO here ."),
)
在此文件中,使用定义的方法 def clean_password2(self)
进一步尝试添加"bio"文件.
Further down in this file within the defined method - def clean_password2(self)
, am trying to add the "bio" as
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
bio = self.cleaned_data.get("bio") # I have no clue how to do this ?
print(bio) #prints None in the terminal
我确实知道没有名称为"bio"的钥匙.在DICT中-cleaned_data.
I do understand there is no key by the name "bio" within the DICT - cleaned_data.
在文件-/site-packages/django/contrib/auth/models.py
中,添加了"bio"作为 models.TextField
,在类 class AbstractUser(AbstractBaseUser,PermissionsMixin)中:
In the file - /site-packages/django/contrib/auth/models.py
, have added the "bio" as a models.TextField
, within the class class AbstractUser(AbstractBaseUser, PermissionsMixin):
bio = models.TextField(
_('bio'),
max_length=500, blank=True)
自定义字段生物"在注册表单中显示,测试用户输入一个文本值-表单被提交,但在psql中没有保存"bio".已注册一个新用户,并且可以在终端的-psql中以及在Django Admin中看到该新用户.
The custom field "bio" shows up in the Registration form , the test user enters a text value - the form gets submitted , but nothing gets saved in the psql for "bio". A new user is registered and can be seen both in - psql at the terminal and within the Django Admin .
也在Django管理员中-当我转到URL时- http://digitalcognition.co.in/admin/auth/user/16/change/,我可以看到一个名为"Bio"的文本字段,在个人信息"中部分,但这又是空白.电子邮件地址","UserName"和密码"像往常一样.
Also within the Django admin - when i go to the URL - http://digitalcognition.co.in/admin/auth/user/16/change/ , i can see a Text Field named "Bio" within the "Personal Info" section , but that again is blank . The "Email Address" , "UserName" and "Password" are seen as usual .
推荐答案
请勿修改默认的用户模型.如文档:
Don't modify the default User model. As mentioned in the documentation:
如果您希望存储与用户有关的信息,则可以使用OneToOneField到包含其他字段的模型信息.这种一对一模型通常称为配置文件模型,例如它可能会存储有关站点用户的与身份验证无关的信息.
If you wish to store information related to User, you can use a OneToOneField to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user.
您绝对不应该修改Django代码.只需创建一个继承自django的UserCreationForm的新表单,然后在其中添加您的字段即可.
And you definitely shouldn't modify django code. Just create a new form that inherits from django's UserCreationForm, and add your fields there.
在此处查看接受的答案.
这篇关于Django-UserModel-如何在django/contrib/auth/forms.py中创建自定义文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!