• <bdo id='Zla1p'></bdo><ul id='Zla1p'></ul>
    <legend id='Zla1p'><style id='Zla1p'><dir id='Zla1p'><q id='Zla1p'></q></dir></style></legend>
    <i id='Zla1p'><tr id='Zla1p'><dt id='Zla1p'><q id='Zla1p'><span id='Zla1p'><b id='Zla1p'><form id='Zla1p'><ins id='Zla1p'></ins><ul id='Zla1p'></ul><sub id='Zla1p'></sub></form><legend id='Zla1p'></legend><bdo id='Zla1p'><pre id='Zla1p'><center id='Zla1p'></center></pre></bdo></b><th id='Zla1p'></th></span></q></dt></tr></i><div id='Zla1p'><tfoot id='Zla1p'></tfoot><dl id='Zla1p'><fieldset id='Zla1p'></fieldset></dl></div>

        <small id='Zla1p'></small><noframes id='Zla1p'>

        <tfoot id='Zla1p'></tfoot>

      1. Django - 如何从模型实例中获取管理员 URL

        Django - How to get admin url from model instance(Django - 如何从模型实例中获取管理员 URL)
      2. <small id='Cpsz0'></small><noframes id='Cpsz0'>

        <i id='Cpsz0'><tr id='Cpsz0'><dt id='Cpsz0'><q id='Cpsz0'><span id='Cpsz0'><b id='Cpsz0'><form id='Cpsz0'><ins id='Cpsz0'></ins><ul id='Cpsz0'></ul><sub id='Cpsz0'></sub></form><legend id='Cpsz0'></legend><bdo id='Cpsz0'><pre id='Cpsz0'><center id='Cpsz0'></center></pre></bdo></b><th id='Cpsz0'></th></span></q></dt></tr></i><div id='Cpsz0'><tfoot id='Cpsz0'></tfoot><dl id='Cpsz0'><fieldset id='Cpsz0'></fieldset></dl></div>
        • <bdo id='Cpsz0'></bdo><ul id='Cpsz0'></ul>
          <tfoot id='Cpsz0'></tfoot>

            <legend id='Cpsz0'><style id='Cpsz0'><dir id='Cpsz0'><q id='Cpsz0'></q></dir></style></legend>

                  <tbody id='Cpsz0'></tbody>

                  本文介绍了Django - 如何从模型实例中获取管理员 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在保存新模型实例时向用户发送电子邮件,并且我希望电子邮件包含指向该模型实例管理页面的链接.有没有办法获得正确的网址?我认为 Django 必须将这些信息存储在某个地方.

                  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.

                  推荐答案

                  这个 Django 片段 应该做:

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

                  self 引用父模型类,即self.id 引用对象的实例id.您也可以将其设置为 property通过将 @property 装饰器粘贴在方法签名之上来建模.

                  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.

                  Chris Pratt 下面 的答案在 ContentType 表.我的回答仍然有效",并且较少依赖于 Django 模型 instance._meta 内部.仅供参考.

                  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.

                  这篇关于Django - 如何从模型实例中获取管理员 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

                    <tfoot id='1li7c'></tfoot>
                      <legend id='1li7c'><style id='1li7c'><dir id='1li7c'><q id='1li7c'></q></dir></style></legend>
                        <tbody id='1li7c'></tbody>
                        <bdo id='1li7c'></bdo><ul id='1li7c'></ul>
                        <i id='1li7c'><tr id='1li7c'><dt id='1li7c'><q id='1li7c'><span id='1li7c'><b id='1li7c'><form id='1li7c'><ins id='1li7c'></ins><ul id='1li7c'></ul><sub id='1li7c'></sub></form><legend id='1li7c'></legend><bdo id='1li7c'><pre id='1li7c'><center id='1li7c'></center></pre></bdo></b><th id='1li7c'></th></span></q></dt></tr></i><div id='1li7c'><tfoot id='1li7c'></tfoot><dl id='1li7c'><fieldset id='1li7c'></fieldset></dl></div>

                            <small id='1li7c'></small><noframes id='1li7c'>