簡體   English   中英

如何登錄用戶模型可在Django中調用

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

我有這個:

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

在display_user函數中是否可以獲取登錄用戶?

我認為您將需要使用threadlocals來存儲request對象,因為在需要的地方Django中並不容易使用該對象。

嘗試這個:
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