【问题标题】:Trying to call is_active method from outside user model in Django ADMIN尝试从 Django ADMIN 中的外部用户模型调用 is_active 方法
【发布时间】: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


【解决方案1】:

动作函数假定查询集将始终包含User 对象,但由于您对ProfileAdmin 使用相同的动作函数,它们还需要处理Profile 对象。

def ban_users(self, request, queryset):
    for obj in queryset:
        if hasattr(obj, 'user'):
            # This object is a Profile, so lookup the user
            obj = obj.user
        obj.is_active = False
        obj.save()
    self.message_user(request, "User is banned and Email has been sent")

注意queryset.update()函数cannot be used to update columns on related models,因此你不能做queryset.update(user__is_active = False)

【讨论】:

    【解决方案2】:

    通过查看您的代码,看看我想到了什么。
    未经检验的理论

    因为is_activeUser 的一个字段,通过queryset 我们可以检查它是UserQueryset 还是Profile

    知道 Profile 对用户有一个传统的OneToOneField,正如我在许多项目中看到的那样

    def ban_users(self, request, queryset):
        # select an instance from queryset
        instance = queryset.first()
        if instance.__class__.__name__ == 'Profile'
            queryset.update(user__is_active = False)
        else:
            queryset.update(is_active = False)
        self.message_user(request, "User is banned and Email has been sent")
    

    【讨论】:

    • 嗯,我遇到了语法错误:if instance.__class__.__name__ == 'Profile' 行上的语法无效,是的,Profile 确实有一个 OneToOneField 到模型中的用户 :)
    • 在函数内添加print(queryset)作为第一行,并显示输出
    • def ban_users(self, request, queryset): print(queryset) #select an instance from queryset 输出仍然是:if instance.__class__.__name__ == 'Profile' ^ SyntaxError: invalid syntax 不幸的是
    • 我的意思是,我只是想知道queryset的值是什么,类型...在你的控制台回溯问题之前,你可以看到print(queryset)的输出向上滚动。如果queryset 像我们以前那样是正常的,我的回答应该有效。
    • Traceback 前只有一行是Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f4f1f6b8bf8> 然后打印回溯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多