Get admin url for a model

1
2
3
4
5
6
from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType

def get_admin_url(self):
    content_type = ContentType.objects.get_for_model(self.__class__)
    return urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(self.id,))

More like this

  1. Front end admin toolbar by eallik 3 years, 5 months ago
  2. Additional Change List Columns by sansmojo 5 years, 10 months ago
  3. Read more link by nny777 4 years, 10 months ago
  4. Admin: return to change_list with filter and pagination applied by fx 2 years, 1 month ago
  5. Redirect with change list with filters intact with admin actions by AndrewIngram 3 years, 10 months ago

Comments

s29 (on October 27, 2010):

Nice. Minor tweak, args=(self.pk,) instead, to support models with explicit primary_key.

#

twoolie (on August 15, 2012):

You can do it without using a query to ContentTypes!

def get_admin_url(self):
    return urlresolvers.reverse("admin:%s_%s_change" %
        (self._meta.app_label, self._meta.module_name), args=(self.id,))

#

(Forgotten your password?)