简体   繁体   English

如何检查用户是否在默认的 django 组中? 在管理面板 (admin.py)

[英]how to check if a user is in a default django group? in the admin panel (admin.py)

My logic is that when a user with is_stuff privilege went to the admin panel and tried to create, for example, news, then the program checked him, in which group he is, if this group exists, then it should issue fieldsets with limited capabilities, or other fields...我的逻辑是,当具有 is_stuff 权限的用户进入管理面板并尝试创建例如新闻时,程序会检查他,他在哪个组中,如果该组存在,那么它应该发布具有有限功能的字段集, 或其他领域...

I checked this link ( In Django, how do I check if a user is in a certain group? ), but it does not work for my situation, since I have to check in the admin panel not return it to api or templates.我检查了此链接( 在 Django 中,如何检查用户是否在某个组中? ),但它不适用于我的情况,因为我必须检查管理面板而不是将其返回到 api 或模板。

i tried to do the following: in admin.py (a)我尝试执行以下操作:在 admin.py (a)

    def get_fieldsets(self, request, obj=None):
       group = Group(name="OutSource")
       if request.user.groups(name=group):
          # first_fields = {...}
       else:
          # second_fields = {...}

callback:打回来:

TypeError: 'NoneType' object is not callable

admin.py (b):管理员.py (b):

if request.user.groups.all()[0].name == "OutSource":

callback:打回来:

IndexError: list index out of range

admin.py (c):管理员.py (c):

if request.user.groups.filter(name='OutSource').exists():

callback:打回来:

unhashable type: 'dict'

I'm sorry, I found a solution to my question.对不起,我找到了解决问题的方法。 Here is the answer if anyone needs it如果有人需要,这是答案

def get_fieldsets(self, request, obj=None):
    users_in_group = Group.objects.get(name="OutSource").user_set.all()
    if request.user in users_in_group:
       # fields = (...)
    else:
       # fields = (...)
    return fields

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

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