views:

938

answers:

4

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'?

A: 

The is no direct way to accomplish this. I see 2 possible solutions.

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

    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')
    
Alex Koshelev
Don't you mean "if you are using simple **non**-threaded Django installation"?
Carl Meyer
Yes, of course. It is my typo.
Alex Koshelev
+2  A: 
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)
Overriding the ModelAdmin's methods is a pretty neat way but the code has some errors: "def highlight_link(self):"should be "def highlight_link(self, obj):"and "super(RadioGridAdmin, self).changelist_view(...)"should have a "return" in front.
Jannis
A: 

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
Erik
globals are dangerous in the context of multi-threaded environments, such as running in mod_phyton on apache server.
Ber
A: 

What's wrong with this:

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

josh