I'm trying to display an absolute URI in the list display of a Django admin:
from django.contrib import admin
class MyAdmin(admin.ModelAdmin):
list_display = (
'full_uri',
)
readonly_fields = (
'full_uri',
)
def full_uri(self, obj):
return u'<a href="{url}">{url}'.format(
url = request.build_absolute_uri(reverse('view_name', args=(obj.pk, )))
)
full_uri.allow_tags = True
The problem - I don't have access to the request object (in which build_absolute_uri
is defined).
I tried to override changelist_view
and store request localy, but this is not thread safe.
I also tried this solution, but it's not very elegant.
Any other ideas?
EDIT:
Is this a safe solution:
def changelist_view(self, request, extra_context=None):
self.request = request
try:
return super(MyAdmin,self).changelist_view(request, extra_context=extra_context)
finally:
self.request = None
EDIT2: