Django - How to get admin url from model instance

前端 未结 5 435
情书的邮戳
情书的邮戳 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:09

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

提交回复
热议问题