Django - How to get admin url from model instance

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

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

提交回复
热议问题