on django admin "changelist" I would like to add a column with a link to…
- User
on django admin "changelist" I would like to add a column with a link to http://myservice/{object.id}
Assistant
You can add a custom column with a link in Django admin's changelist view by defining a method in your ModelAdmin class. Here's how:
from django.utils.html import format_html from django.contrib import admin class YourModelAdmin(admin.ModelAdmin): list_display = ('id', 'name', 'service_link', ...) # Add your custom method to list_display def service_link(self, obj): return format_html('<a href="http://myservice/{}" target="_blank">View in Service</a>', obj.id) # Optional: Add a column header name service_link.short_description = "Service Link" admin.site.register(YourModel, YourModelAdmin)Would you like me to explain how this code works?
- User
The link should only be visible for superuser
Assistant
You can modify the method to only show the link for superusers by checking the user's permissions. Here's how:
from django.utils.html import format_html from django.contrib import admin class YourModelAdmin(admin.ModelAdmin): list_display = ('id', 'name', 'service_link', ...) def service_link(self, obj): # Check if the current user is a superuser if self.request.user.is_superuser: return format_html('<a href="http://myservice/{}" target="_blank">View in Service</a>', obj.id) return "-" # Return a dash or empty string for non-superusers service_link.short_description = "Service Link" # Store the request to access the user in list_display methods def changelist_view(self, request, extra_context=None): self.request = request return super().changelist_view(request, extra_context) admin.site.register(YourModel, YourModelAdmin)Would you like me to explain how this code works?
- User
is there a get_list_display so you only show the column to superusers?
Assistant
Yes, you can use
get_list_displayto dynamically change which columns are shown based on the user. This is a cleaner approach than showing a dash for non-superusers:from django.utils.html import format_html from django.contrib import admin class YourModelAdmin(admin.ModelAdmin): # Define base list_display without the service_link list_display = ('id', 'name', ...) def get_list_display(self, request): list_display = list(self.list_display) # Add service_link only for superusers if request.user.is_superuser: list_display.append('service_link') return list_display def service_link(self, obj): return format_html('<a href="http://myservice/{}" target="_blank">View in Service</a>', obj.id) service_link.short_description = "Service Link" admin.site.register(YourModel, YourModelAdmin)Would you like me to explain how this code works?
- User
now instead of an arbitrary link, it is a django view, myapp:mylink, path is 'myapp/int:object_id'
Assistant
Here's how to create a link to a Django view with the object ID as a parameter:
from django.utils.html import format_html from django.urls import reverse from django.contrib import admin class YourModelAdmin(admin.ModelAdmin): # Define base list_display without the service_link list_display = ('id', 'name', ...) def get_list_display(self, request): list_display = list(self.list_display) # Add service_link only for superusers if request.user.is_superuser: list_display.append('service_link') return list_display def service_link(self, obj): # Generate URL using reverse with the view name and object ID url = reverse('myapp:mylink', kwargs={'object_id': obj.id}) return format_html('<a href="{}" target="_blank">View in Service</a>', url) service_link.short_description = "Service Link" admin.site.register(YourModel, YourModelAdmin)Would you like me to explain how this code works?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.