每当我在admin中添加新用户时,如何在django管理页面中添加id_card_number输入字段?

时间:2019-07-31 11:52:14

标签: python html django

我有一个问题要搜索很多天,​​但找不到答案。我希望在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>

1 个答案:

答案 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()

然后应用makemigrationsmigrate。这将在用户模型中添加 Id_card_number 列。