Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 991
Erik Avatar asked Apr 07 '09 23:04

Erik


People also ask

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

What is Admin Modeladmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.

How do I give permission to admin in Django?

Test the 'view' permission is added to all modelsUsing #3 for Django 1.7 only creates the permission objects if the model doesn't already exist. Is there a way to create a migration (or something else) to create the permission objects for existing models?

What does admin py do in Django?

The admin.py file is used to display your models in the Django admin panel. You can also customize your admin panel. Hope this helps you.


1 Answers

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

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 
like image 171
Diego Puente Avatar answered Sep 28 '22 15:09

Diego Puente