我有一个问题要搜索很多天,但找不到答案。我希望在django管理页面中单击Id_card_number
时,新字段以add user
的名称出现。请记住,Id_card_number
出现在注册页面中,但没有出现在django管理页面中。请帮忙。
forms.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)
Id_card_number = forms.CharField(max_length=15, required=True)
class Meta:
model = User
fields = ['username','email','password1','password2','Id_card_number']
register.html
<form method="POST">
{%csrf_token%}
<fieldset class="form-group">
<legend class="border-bottom mb-4" style="font-size: 50px">Join Today</legend><br>
{{form|crispy}}<br>
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Sign Up</button>
</div>
答案 0 :(得分:0)
首先,您必须通过扩展django的默认用户模型在用户模型中添加Id_card_number
列。
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
Id_card_number = forms.CharField(max_length=15, required=True)
settings.py
添加此内容:
AUTH_USER_MODEL = 'your-app-name.User'
admin.py
from django.contrib.auth.admin import UserAdmin
from .models import *
admin.site.register(User, UserAdmin)
views.py
添加此内容:
User = get_user_model()
然后应用makemigrations
和migrate
。这将在用户模型中添加 Id_card_number 列。