Django Admin:如何访问 admin.py 中的 request 对象,用于 list_display 方法?
我在模型的 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)
它返回一个自定义链接(为了简洁起见,我省略了 highlight_link.short_description
)返回的每条记录在更改列表中。 这太棒了。 但我想检查当前的查询字符串并根据该字符串更改自定义链接。 有没有办法访问 highlight_link
中的请求对象?
I've added a method highlight_link
to my model's admin.py class:
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. 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我用这种方式解决了我的问题(对于 1.7 以下的 django):
现在我可以在任何地方使用
self.request
UPDATE
I solve my issue this way (for django under 1.7):
Now i can use
self.request
in any placeUPDATE
描述了如何将其重构为 mixin ,并根据 @taha-jahangir 的答案添加了线程安全位。 这是 mixin:
Subclass ModelAdmin with the mixin:
... 并且您可以从任何方法调用
self.get_request()
。Described how to refactor this into a mixin, with addition of the thread-safety bit based on @taha-jahangir's answer. Here's the mixin:
Subclass ModelAdmin with the mixin:
... and you can just call
self.get_request()
from any method.小代码澄清了Diego Puente答案(python 3.6):
因此您可以从
MyClassAdmin
的任何其他方法获取self.request
。如果在
get_queryset
方法中定义self.request
(未在__init__
中声明),PyCharm 将生成警告Instance attribute attribute_name在 __init__ 之外定义< /代码>。
Small code clarify for Diego Puente answer (python 3.6):
So you can get
self.request
from any other method ofMyClassAdmin
.If define
self.request
inget_queryset
method (without declaring it in__init__
) PyCharm will generate warningInstance attribute attribute_name defined outside __init__
.没有直接的方法可以实现这一点。 我看到两种可能的解决方案。
使用线程局部变量存储到同一请求对象
如果您使用简单的非线程 Django 安装,则可以将请求对象保存为属性:
The is no direct way to accomplish this. I see 2 possible solutions.
Use a thread locals store to same request object
If you are using simple non-threaded Django installation it is possible to save request object just as attribute:
这是 @user27478 答案的编辑版本,它使用线程本地变量:
This is edited version of @user27478 answer, which uses thread-local vars:
我尝试了这里留下的其他答案,但遇到了对我来说变得越来越复杂的问题。 我尝试了一下 def __call__() 并得出了以下结论。 这可能不是执行此操作的正确方法,但它有效...
在此处获取 GET 变量(全部在 RadioGridAdmin 类中,如我最初的文章中所述):
并且由于它是全局的,因此您现在可以在此处访问它:
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. 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):
and since it's global, you can now access it here:
admin_mixins.py
any_app/admin.py
来源: https://gist.github.com/pricco/24826bae3d5102d963eb13ecc0493f33< /a>
admin_mixins.py
any_app/admin.py
Source: https://gist.github.com/pricco/24826bae3d5102d963eb13ecc0493f33
这是什么问题:
What's wrong with this: