Login

Get admin url for a model

Author:
timbroder
Posted:
February 9, 2010
Language:
Python
Version:
1.1
Tags:
admin url
Score:
3 (after 3 ratings)

Add this to your model to be able to get their admin change link from anywhere

Useful if you want to jump to the admin screen of an object you are looking at on the front end

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. Application-independent permalink to admin url for an object by dchandek 7 years ago
  2. Generate a CSV file for a model by mssnlayam 7 years, 11 months ago
  3. Automatic urls for static pages by jibbolo 4 years, 3 months ago
  4. models with order (+admin editing) by bram 6 years, 6 months ago
  5. instant 'master' admin site by dschein2 7 years, 7 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,))

#

Please login first before commenting.