为什么我的用户模型在django admin

时间:2018-08-27 11:20:42

标签: python django

我正在使用Django 2.0.8和Python 3.5

我有以下代码

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser, UserManager
from django.utils.timezone import now


# Create your models here.
class SiteMemberManager(UserManager):
    pass


class SiteMember(AbstractUser):
    objects = SiteMemberManager()

    MEMBER_STATUS = (
        ('1', 'Pending Approval'),
        ('2', 'Approved'),
        ('3', 'Banned'),
    )

    status = models.CharField(max_length=16, choices=MEMBER_STATUS)    

    last_seen = models.DateTimeField(null=False, default=now)
    abuse_report_count = models.PositiveSmallIntegerField(null=False, default=0)    

    fields = ('last_seen', 'abuse_report_count')

admin.py

from django.contrib import admin

# Register your models here.
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import SiteMemberCreationForm, SiteMemberChangeForm
from .models import SiteMember


class SiteMemberAdmin(UserAdmin):
    model = SiteMember
    add_form = SiteMemberCreationForm
    form = SiteMemberChangeForm


admin.site.register(SiteMember, SiteMemberAdmin)

当我进入django管理页面时,有两个问题:

  1. 我的模型SiteMember未出现在django应用程序下(显示的模型为Users
  2. Users模型不会显示我在SiteMember模型上实现的任何字段

我已经运行了python manage.py makemigrationspython manage.py migrate,并且已经应用​​了迁移(根据控制台输出)。

我该如何解决?

[[澄清]] 我已经做了以下事情:

  • 重新启动应用程序
  • 在INSTALLED_APPS(settings.py)中安装了我的新应用
  • settings.AUTH_USER_MODEL已更新为引用新的(自定义)用户

2 个答案:

答案 0 :(得分:0)

docs中所述,您可以将字段更改为显示,过滤器,订购等。

示例

from django.contrib.auth.models import User

class SiteMemberAdmin(UserAdmin):
    model = SiteMember
    add_form = SiteMemberCreationForm
    form = SiteMemberChangeForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
    list_display = ('email', 'date_of_birth', 'is_admin')
    list_filter = ('is_admin',)
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('date_of_birth',)}),
        ('Permissions', {'fields': ('is_admin',)}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'date_of_birth', 'password1', 'password2')}
        ),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

admin.site.register(SiteMember, SiteMemberAdmin)

答案 1 :(得分:-1)

这可能是因为您继承自UserAdmin类而不是ModelAdmin。

parentEpic = EPIC-123