我正在使用Django 2.0.8和Python 3.5
我有以下代码
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')
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管理页面时,有两个问题:
SiteMember
未出现在django应用程序下(显示的模型为Users
Users
模型不会显示我在SiteMember
模型上实现的任何字段我已经运行了python manage.py makemigrations
和python manage.py migrate
,并且已经应用了迁移(根据控制台输出)。
我该如何解决?
[[澄清]] 我已经做了以下事情:
答案 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