Django - How to get admin url from model instance

前端 未结 5 431
情书的邮戳
情书的邮戳 2021-02-01 15:34

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

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 16:35

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

提交回复
热议问题