Django – How to get admin url from model instance

Not trying to rip off @JosvicZammit, but using ContentType is the wrong approach here. It’s just a wasted DB query. You can get the require info from the _meta attribute:

from django.urls import reverse

info = (model_instance._meta.app_label, model_instance._meta.model_name)
admin_url = reverse('admin:%s_%s_change' % info, args=(model_instance.pk,))

Leave a Comment