6

Here I made an example of what I have in my admin.py:

@admin.register(Car)
class CarAdmin(CustomAdmin):
    list_display = ('get_color',)
    def get_color(self, obj):
        return mark_safe('<a href="/admin/myapp/car/?color={}">{}</a>'.format(obj.color, obj.color))

I have produced a link that can be used to display cars with a specific color. Let's say the page is currently showing cars that cost less than $20k and I want this link to preserve the current filter. Any ideas how to do that? Maybe there is a way to get the current url from python.

Note that I know I can write a javascript to modify the links after the page is loaded, but that is a terrible solution.

1 Answer 1

9

You can save the full path or request before the get_color is called, something like:

class CarAdmin(CustomAdmin):
    list_display = ('get_color',)

    def get_queryset(self, request):
        self.full_path = request.get_full_path()
        return super().get_queryset(request)

    def get_color(self, obj):
        # TODO: Handle empty query parameters 
        return mark_safe(f'<a href="{self.full_path}&color={obj.color}">{obj.color}</a>')  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.