Reputation: 5174
If there are two models
models.py
class ModelA(models.Model):
pass
class ModelB(models.Model):
a = models.ForeignKey(ModelA, null=True)
admin.py
class ModelBAdmin(admin.ModelAdmin):
list_display = ('id', 'a', )
list_display_links = None
ordering = ('-id', )
admin.site.register(ModelB, ModelBAdmin)
class ModelAAdmin(admin.ModelAdmin):
list_display = ('id', )
list_display_links = None
admin.site.register(ModelA, ModelAAdmin)
Is there a way to link a
with its detail view in list view of ModelB
? If not, then can we do the same in detail view of ModelB
? That is, display a
as a link to its detail view?
Currently in list view of ModelB
there is only id
of ModelA
. And to view its detail I have to go to ModelA
list view and search for the id. What I want id
in list view of ModelB
to point to detail view of a
(like /admin/ModelA/{a.id})
Upvotes: 3
Views: 1076
Reputation: 886
The fields in the list view can be callables, so you can do this:
from django.core.urlresolvers import reverse
class ModelBAdmin(admin.ModelAdmin):
list_display = ('id', 'a_link', )
list_display_links = None
ordering = ('-id', )
def a_link(obj):
return format_html('<a href="{}">{}</a>', reverse(
'admin:myapp_a_change', args=[obj.a.id]), obj.a.name)
a_link.short_description = "Model A Details"
admin.site.register(ModelB, ModelBAdmin)
Upvotes: 2