Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219· AnthropicHugging Face LMArena Human Preference 140K

on django admin "changelist" I would like to add a column with a link to…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-v3-0324’s answer.
  1. User

    on django admin "changelist" I would like to add a column with a link to http://myservice/{object.id}

  2. Claude 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?

  3. User

    The link should only be visible for superuser

  4. Claude 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?

  5. User

    is there a get_list_display so you only show the column to superusers?

  6. Claude Assistant

    Yes, you can use get_list_display to 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?

  7. User

    now instead of an arbitrary link, it is a django view, myapp:mylink, path is 'myapp/int:object_id'

  8. Claude 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?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.