41

I'm trying to send an email to a user when a new model instance is saved and I want the email to include a link to the admin page for that model instance. Is there a way to get the correct URL? I figure Django must have that information stored somewhere.

5 Answers 5

68

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,))
0
48

This Django snippet should do:

from django.urls import reverse
from django.contrib.contenttypes.models import ContentType
from django.db import models

class MyModel(models.Model):

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

The self refers to the parent model class, i.e. self.id refers to the object's instance id. You can also set it as a property on the model by sticking the @property decorator on top of the method signature.

EDIT: The answer by Chris Pratt below saves a DB query over the ContentType table. My answer still "works", and is less dependent on the Django model instance._meta internals. FYI.

3
  • These answers don't seem to be working. I just get back "/admin/tracker/order/2/". How do I get the hostname?
    – Greg
    May 9, 2012 at 19:43
  • @Greg Of course, get_admin_url() above gets you the part that goes after the hostname. Use the Django sites framework to appropriately get the hostname of the current site. Then prefix it to the result of get_admin_url(). May 9, 2012 at 20:48
  • 1
    Works fine in 1.8, too May 27, 2015 at 10:55
13

This gives the same result as Josvic Zammit's snippet, but does not hit the database:

from django.urls import reverse
from django.db import models

class MyModel(models.Model):

    def get_admin_url(self):
        return reverse("admin:%s_%s_change" % (self._meta.app_label, self._meta.model_name), args=(self.id,))
5
  • change attr module_name by model_name (module_name is undefined)
    – ppython
    Aug 15, 2017 at 15:49
  • 1
    Django 1.8 renamed the 'module_name' attribute to 'model_name' in a Model's meta object. Code snippet updated ... thank you ! Aug 19, 2017 at 15:55
  • What if I have several AdminSite instances? How to get current one instead of admin:?
    – EugZol
    Jan 11, 2021 at 17:19
  • @EugZol, I would assign a name to each admin, then use it in reverse(). For example, in urlspatterns: path('admin_ex/', admin_ex.urls, name="admin_ex"); then reverse('admin_ex:label_model_action'). Never had the need to use this in practice, however. Jan 11, 2021 at 18:06
  • @MarioOrlandi Thanks!
    – EugZol
    Jan 11, 2021 at 18:27
12

Just use this one liner that is also python 3 ready:

from django.urls import reverse

reverse('admin:{0}_{1}_change'.format(self._meta.app_label, self._meta.model_name), args=(self.pk,))

More on this in the django admin site doc, reversing admin urls.

3
  • 1
    Should be self._meta.model_name rather than self._meta.module_name.
    – nedned
    May 25, 2017 at 8:17
  • Thanks @nedned, fixing
    – radtek
    May 25, 2017 at 14:12
  • 2
    Just because it’s possible: reverse('admin:{m.app_label}_{m.model_name}_change'.format(m=self._meta), args=[self.pk])
    – batisteo
    Mar 8, 2018 at 21:15
5

So, combining the answers by Chris, Josvic and Josh, here's a copy-paste method you can add into your model (tested on Django 1.8.3).

def get_admin_url(self):
    """the url to the Django admin interface for the model instance"""
    from django.core.urlresolvers import reverse
    info = (self._meta.app_label, self._meta.model_name)
    return reverse('admin:%s_%s_change' % info, args=(self.pk,))

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.