[英]Django Admin: How to access the request object in admin.py, for list_display methods?
I've added a method highlight_link
to my model's admin.py class:我在模型的 admin.py 类中添加了一个方法
highlight_link
:
class RadioGridAdmin(admin.ModelAdmin):
list_display = ('start_time', highlight_link)
def highlight_link(self):
return ('some custom link')
admin.site.register(RadioGrid, RadioGridAdmin)
It returns a custom link for (I've left out highlight_link.short_description
for brevity) each record returned in the change list.它为更改列表中返回的每条记录返回一个自定义链接(为简洁起见,我省略了
highlight_link.short_description
)。 Which is great.这很棒。 But I'd like to inspect the current query string and change the custom link based on that.
但我想检查当前的查询字符串并根据它更改自定义链接。 Is there a way to access the request object within
highlight_link
?有没有办法在
highlight_link
访问请求对象?
I solve my issue this way.我是这样解决我的问题的。
class MyClassAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(MyClassAdmin, self).queryset(request)
self.request = request
return qs
Now i can use self.request
in any place现在我可以在任何地方使用
self.request
UPDATE更新
Changed in Django 1.6: The get_queryset method was previously named queryset.
在 Django 1.6 中更改:get_queryset 方法以前被命名为 queryset。
class MyClassAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super(MyClassAdmin, self).get_queryset(request)
self.request = request
return qs
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)
Described how to refactor this into a mixin , with addition of the thread-safety bit based on @taha-jahangir's answer.描述了如何将其重构为 mixin ,并根据@taha-jahangir 的回答添加了线程安全位。 Here's the mixin:
这是混合:
import threading
class ModelAdminRequestMixin(object):
def __init__(self, *args, **kwargs):
# let's define this so there's no chance of AttributeErrors
self._request_local = threading.local()
self._request_local.request = None
super(ModelAdminRequestMixin, self).__init__(*args, **kwargs)
def get_request(self):
return self._request_local.request
def set_request(self, request):
self._request_local.request = request
def changeform_view(self, request, *args, **kwargs):
# stash the request
self.set_request(request)
# call the parent view method with all the original args
return super(ModelAdminRequestMixin, self).changeform_view(request, *args, **kwargs)
def add_view(self, request, *args, **kwargs):
self.set_request(request)
return super(ModelAdminRequestMixin, self).add_view(request, *args, **kwargs)
def change_view(self, request, *args, **kwargs):
self.set_request(request)
return super(ModelAdminRequestMixin, self).change_view(request, *args, **kwargs)
def changelist_view(self, request, *args, **kwargs):
self.set_request(request)
return super(ModelAdminRequestMixin, self).changelist_view(request, *args, **kwargs)
def delete_view(self, request, *args, **kwargs):
self.set_request(request)
return super(ModelAdminRequestMixin, self).delete_view(request, *args, **kwargs)
def history_view(self, request, *args, **kwargs):
self.set_request(request)
return super(ModelAdminRequestMixin, self).history_view(request, *args, **kwargs)
Subclass ModelAdmin with the mixin:使用 mixin 子类 ModelAdmin:
class PollAdmin(ModelAdminRequestMixin, admin.ModelAdmin):
pass
... and you can just call self.get_request()
from any method. ...你可以从任何方法调用
self.get_request()
。
Small code clarify for Diego Puente answer (python 3.6):迭戈普恩特答案的小代码澄清(python 3.6):
class MyClassAdmin(admin.ModelAdmin):
def __init__(self, model, admin_site):
self.request = None
super().__init__(model, admin_site)
def get_queryset(self, request):
self.request = request
return super().get_queryset(request)
So you can get self.request
from any other method of MyClassAdmin
.所以,你可以得到
self.request
从任何其他方法MyClassAdmin
。
If define self.request
in get_queryset
method (without declaring it in __init__
) PyCharm will generate warning Instance attribute attribute_name defined outside __init__
.如果在
get_queryset
方法中定义self.request
(而不在__init__
声明),PyCharm 将生成在__init__
Instance attribute attribute_name defined outside __init__
警告Instance attribute attribute_name defined outside __init__
。
The is no direct way to accomplish this.没有直接的方法来实现这一点。 I see 2 possible solutions.
我看到了 2 种可能的解决方案。
Use a thread locals store to same request object使用线程本地存储到相同的请求对象
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')
If you are using simple non-threaded Django installation it is possible to save request object just as attribute:如果您使用简单的非线程 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')
This is edited version of @user27478 answer, which uses thread-local vars:这是@user27478 答案的编辑版本,它使用线程本地变量:
class RadioGridAdmin(admin.ModelAdmin):
def __init__(self, model, admin_site):
super().__init__(model, admin_site)
self._request_local = threading.local()
def changelist_view(self, request, extra_context=None):
self._request_local.request = request
return super().changelist_view(request, extra_context)
@property
def _request(self):
return self._request_local.request
def example_highlight_link(self, obj):
changelist = self.get_changelist_instance(self._request)
url = changelist.get_query_string(new_params={'key1': 1})
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()
# ...
from functools import partial, update_wrapper, lru_cache
# Django admin call 2 times get_list_display.
# We need to return the same function to make the method sortable using 'admin_order_field'
# https://github.com/django/django/blob/2161db0792f2e4d3deef3e09cd72f7a08340cafe/django/contrib/admin/templatetags/admin_list.py#L84
@lru_cache(maxsize=100)
def cache_display_wrap(f, request):
wf = partial(f, request)
nf = update_wrapper(wf, f)
return nf
class ModelAdminMixin(admin.ModelAdmin):
def get_list_display(self, request):
def check_needs_request(display):
f = getattr(self, display, None) if not callable(display) else display
if f and getattr(f, 'needs_request', False):
return cache_display_wrap(f, request)
return display
return [check_needs_request(display) for display in super().get_list_display(request)]
from django.contrib import admin
from core.admin_mixins import ModelAdminMixin
@admin.register(AnyModel)
class AnyModelAdmin(ModelAdminMixin, admin.ModelAdminMixin):
list_display = ['id', 'especial_display_with_request']
def especial_display_with_request(self, request, obj):
# Make something special with the request
return obj.any_field
especial_display_with_request.needs_request = True # Similar to short_description or any other django admin attr.
Source:https://gist.github.com/pricco/24826bae3d5102d963eb13ecc0493f33来源:https ://gist.github.com/pricco/24826bae3d5102d963eb13ecc0493f33
I tried the other answers left here and ran into issues that for me, were getting complex.我尝试了这里留下的其他答案,但遇到了对我来说越来越复杂的问题。 I played around with
def __call__()
and came up with the following.我玩弄了
def __call__()
并想出了以下内容。 This probably isn't the correct way to do this, but it works...这可能不是正确的方法,但它有效......
grab the GET variable here (all within class RadioGridAdmin as described above in my initial post):在这里获取 GET 变量(都在 RadioGridAdmin 类中,如我最初的帖子中所述):
def __call__(self, request, url):
global start_date
start_date = request.GET['param']
return super(RadioGridAdmin, self).__call__(request, url)
and since it's global, you can now access it here:由于它是全球性的,您现在可以在这里访问它:
def highlight_link(self):
# access start_date here
What's wrong with this:这有什么问题:
def highlight_link(self, request):
# access start_date here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.