2009-04-07 409 views
15

我添加了一個方法「highlight_link」我的模型admin.py類:Django Admin:如何訪問admin.py中的請求對象,以獲取list_display方法?

class RadioGridAdmin(admin.ModelAdmin): 

    list_display = ('start_time', highlight_link) 

    def highlight_link(self): 
     return ('some custom link') 


admin.site.register(RadioGrid, RadioGridAdmin) 

它返回一個自定義鏈接(我已經離開了爲簡潔highlight_link.short_description)每個記錄的變化返回名單。這很棒。但我想檢查當前的查詢字符串,並根據此更改自定義鏈接。有沒有辦法在'highlight_link'中訪問Request對象?

+0

我想指出,這種相關票證使用self.request .djangoproject.com/ticket/13659 – Paolo 2016-05-14 09:12:19

回答

0

我試過了其他答案留在這裏,遇到的問題對我來說變得越來越複雜。我玩了def __call__(),想出了以下內容。這可能不是這樣做的正確的方式,但它的作品...

搶GET變量這裏(內如上面我最初的職位描述類RadioGridAdmin全部):

def __call__(self, request, url): 
    global start_date 
    start_date = request.GET['param'] 

    return super(RadioGridAdmin, self).__call__(request, url) 

和自這是全球性的,你可以在這裏現在訪問:

def highlight_link(self): 
    # access start_date here 
3

沒有直接的方法來實現這一點。我看到2種可能的解決方案

  • 使用一個線程當地人商店同一請求對象

    from django.utils._threading_local import locals 
    
    globals = locals() 
    
    class RadioGridAdmin(admin.ModelAdmin): 
        def __call__(self, request, *args, **kwargs): 
         globals['radio_grid_admin_request'] = request 
         return super(RadioGridAdmin, self).__call__(request, *args, **kwargs) 
    
        def highlight_link(self): 
         request = globals['radio_grid_admin_request'] 
         # request.GET processing 
         return ('some custom link') 
    
  • 如果使用簡單的非線程的Django安裝,可以請求對象就像儲存屬性:

    class RadioGridAdmin(admin.ModelAdmin): 
        def __call__(self, request, *args, **kwargs): 
         self.request = request 
         return super(RadioGridAdmin, self).__call__(request, *args, **kwargs) 
    
        def highlight_link(self): 
         # self.request.GET processing 
         return ('some custom link') 
    
+0

你不是指「如果你使用簡單的**非** - thr使用Django安裝「? – 2009-04-08 10:30:31

+0

當然可以。這是我的錯字。 – 2009-04-08 20:20:18

+0

使用這兩種場景中的一種或另一種會有什麼影響? – Gelbander 2015-12-16 13:37:29

15
class RadioGridAdmin(admin.ModelAdmin): 

    def highlight_link(self, obj): 
     return (self.param) 

    def changelist_view(self, request, extra_context=None): 
     self.param = request.GET['param'] 
     return super(RadioGridAdmin,self).changelist_view(request, extra_context=extra_context) 
+0

重寫的ModelAdmin的方法是一種非常簡潔的方式,但代碼中有一些錯誤: 「高清highlight_link(個體經營):」 應該是 「高清highlight_link(個體經營,OBJ):」 和 「超級(RadioGridAdmin,self).changelist_view(...)「 應該在前面有一個」返回「。 – jnns 2010-04-27 11:34:21

-2

有什麼不對的:

def highlight_link(self, request): 
    # access start_date here 
11

我SOLV這是我的問題。 HTTPS://代碼

class MyClassAdmin(admin.ModelAdmin): 

    def queryset(self, request): 
     qs = super(MyClassAdmin, self).queryset(request) 
     self.request = request 
     return qs 

現在我可以在任何地方

UPDATE

Changed in Django 1.6: The get_queryset method was previously named queryset.

class MyClassAdmin(admin.ModelAdmin): 

    def get_queryset(self, request): 
     qs = super(MyClassAdmin, self).get_queryset(request) 
     self.request = request 
     return qs 
0
import threading 

_thread_local = threading.local() 

def get_thread_local_request(): 
    return getattr(_thread_local, "request", None) 

class RadioGridAdmin(admin.ModelAdmin): 
    list_display = ('display_field', ...) 

    def display_field(self, obj): 
     # ... 
     request = get_thread_local_request() 
     # ... 
相關問題