简体   繁体   English

如何登录用户模型可在Django中调用

[英]How to get logged in user modelAdmin callable in django

I have this: 我有这个:

class SummaryAdmin(admin.ModelAdmin):
    list_display = ('date', 'display_user', 'from_till', 'hours_worked',
                    'productivity_status',)
    list_filter = ['date', 'user_crm']

    def display_user(self, obj):
        test = "unknown"
        temp = obj.user_crm

        #if user.has_perm('crm.list_all_customers'):
        if temp.user:
            first_name = temp.user.first_name
            last_name = temp.user.last_name
            test = "%s %s" % (first_name, last_name)
        elif temp.alternate:
            test = "%s " % temp.alternate
        else:
            test = "%s (not linked)" % obj.user

        #return obj.salesperson if obj.salesperson is not None else ''
        return test

is it possible to get the logged in user while in the display_user function? 在display_user函数中是否可以获取登录用户?

I think you will need to use threadlocals to store the request object, as it is not readily available in Django at the place where you need it. 我认为您将需要使用threadlocals来存储request对象,因为在需要的地方Django中并不容易使用该对象。

Try this: 尝试这个:
https://github.com/nebstrebor/django-threadlocals https://github.com/nebstrebor/django-threadlocals

from threadlocals.threadlocals import get_current_user

class SummaryAdmin(admin.ModelAdmin):
    list_display = ('date', 'display_user', 'from_till', 'hours_worked',
                    'productivity_status',)
    list_filter = ['date', 'user_crm']

    def display_user(self, obj):
       current_user = get_current_user()
       ...

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

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