【发布时间】:2018-05-03 02:19:32
【问题描述】:
这里我有一个禁止和取消禁止用户的功能。目前我可以在自定义用户模型中的 DjangoAdmin 中使用它。 但我想做的是在我的 DjangoAdmin 中禁止用户使用 Profile 模型。
但我不断收到 ERROR FieldDoesNotExist at /admin/api/profile/
配置文件没有名为“is_active”的字段,因为is_active 是用户模型的功能。我不知道如何在管理员的配置文件模型视图中禁止用户或如何连接is_active 到Profile 模型。
#Banning Function
def ban_users(self, request, queryset):
queryset.update(is_active = False)
self.message_user(request, "User is banned and Email has been sent")
def remove_ban(self, request, queryset):
queryset.update(is_active = True)
self.message_user(request, "Users ban has been lifted")
#Admin functions will be created here,as well as registration of their specific models
class ReportAdmin(admin.ModelAdmin):
list_display = ('user_reported', 'report_reason', 'sent_by', 'session')
admin.site.register(Report, ReportAdmin)
class ProfileAdmin(admin.ModelAdmin):
list_display = ('user', 'birth_date', 'sessions_played', 'total_reports')
readonly_fields = (('sessions_played'),('birth_date'),('user'),('pref_server'),('teamwork_commends'),('skill_commends'),('sportsmanship_commends'),('communication_commends'),('discord_name'))#,'total_reports')
actions = ['ban', 'unban']
ban = ban_users
unban = remove_ban
def total_reports(self, obj):
return Report.objects.filter(user_reported=obj).count()
admin.site.register(Profile, ProfileAdmin)
class MyUserAdmin(UserAdmin):
list_display = ('username', 'first_name', 'last_name' , 'email')
readonly_fields = ('first_name' , ('last_name') , ('email') , ('username'))
actions = ['ban', 'unban']
ban = ban_users
unban = remove_ban
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
【问题讨论】:
-
可以加
Profile型号吗?
标签: python django django-admin