Django Admin: How to access the request object in admin.py, for list_display methods?

后端 未结 9 1705
太阳男子
太阳男子 2020-12-13 14:30

I\'ve added a method highlight_link to my model\'s admin.py class:

class RadioGridAdmin(admin.ModelAdmin):
    
    list_display = (\'start_time\         


        
9条回答
  •  無奈伤痛
    2020-12-13 15:09

    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):

    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
    

提交回复
热议问题