简体   繁体   English

admin.py中function里面的request.user.is_superuser怎么正确

[英]How properly request.user.is_superuser inside function in django admin.py

I am learning Djnago and I have this function inside admin.py that counts how many participants in an activity, it is working fine until, I want to know if the current user is a superuser and added request inside def set_count(self, request, obj) expecting to make a request for the user.我正在学习 Djnago 并且我在 admin.py 中有这个 function 来计算活动中有多少参与者,它工作正常,直到我想知道当前用户是否是超级用户并在def set_count(self, request, obj) request def set_count(self, request, obj)期望为用户发出请求。 Since I am getting an error, this means, this it's not how to do it and wrong.因为我收到一个错误,这意味着,这不是怎么做和错误的。 How to correct this?如何纠正这个? Thanks.谢谢。

Below is what I want to do, evaluate the user.is_superuser if true, it will display in the list_display the link to the participants_changelist otherwise, display a plain text.下面是我想要做的,评估user.is_superuser如果为真,它将在list_display中显示到participants_changelist的链接,否则显示纯文本。

def get_queryset(self, request):
    queryset = super().get_queryset(request)
    queryset = queryset.annotate(set_count=Count('activity_activity'))
    return queryset

def set_count(self, request, obj):
    
    counter =  obj.set_count
    if request.user.is_superuser:
        
        url = (
            reverse("admin:activity_announcements_participants_changelist")
            + "?"
            + urlencode({"activity__id": f"{obj.id}"})
        )
    
        return format_html('<a href="{}">{} Paticipants</a>', url, counter)
    else:
        counter =  obj.set_count
        return format_html('{} Paticipants', counter)

set_count.short_description = "No. of Paticipants"

Error:错误:

TypeError at /admin/activity_announcements/activity/
ActivityAdmin.set_count() missing 1 required positional argument: 'obj'
Request Method: GET
Request URL:    http://localhost:8000/admin/activity_announcements/activity/
Django Version: 4.1.5
Exception Type: TypeError
Exception Value:    
ActivityAdmin.set_count() missing 1 required positional argument: 'obj'
Exception Location: /py/lib/python3.11/site-packages/django/contrib/admin/utils.py, line     280, in lookup_field
Raised during:  reversion.admin.changelist_view
Python Executable:  /py/bin/python
Python Version: 3.11.1
Python Path:    
['/app',
'/usr/local/lib/python311.zip',
'/usr/local/lib/python3.11',
'/usr/local/lib/python3.11/lib-dynload',
'/py/lib/python3.11/site-packages',
'/py/lib/python3.11/site-packages/odf',
'/py/lib/python3.11/site-packages/odf',
'/py/lib/python3.11/site-packages/odf',
'/py/lib/python3.11/site-packages/odf',
'/py/lib/python3.11/site-packages/odf',
'/py/lib/python3.11/site-packages/odf',
'/py/lib/python3.11/site-packages/odf']
Server time:    Sun, 22 Jan 2023 03:06:22 +0800

The problem is that when defining a custom field to list in the admin, you don't have access to the request.问题是,在定义要在管理员中列出的自定义字段时,您无权访问该请求。 The function definition should be like this def your_field(self, obj): . function 的定义应该是这样的def your_field(self, obj): In order to get the request in a field definition you can do the following:为了在字段定义中获取请求,您可以执行以下操作:

    class YourAdmin(admin.modeladmin):
        def get_queryset(self, request):
            self.request = request
            return super().get_queryset(request)

    def set_count(self, obj):
    
        counter =  obj.set_count

        if self.request.user.is_superuser:
        
            url = (
            reverse(
             "admin:activity_announcements_participants_changelist")
             + "?"
             + urlencode({"activity__id": f"{obj.id}"})
            )
    
            return format_html('<a href="{}">{} Participants</a>', url, counter)
        else:
            counter =  obj.set_count
        return format_html('{} Participants', counter)
    set_count.short_description = "No. of Paticipants"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM