Django—如何从模型实例获取管理url

时间:2021-05-09 22:56:44

First of all, thank you for reading my question.

首先,感谢大家阅读我的问题。

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.

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

5 个解决方案

#1


27  

This Django snippet should do:

这个Django代码片段应该如下:

from django.core import urlresolvers
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 urlresolvers.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.

self指的是父模型类,即self。id指的是对象的实例id,您还可以将其设置为模型上的属性,方法签名的顶部粘贴@property decorator。

#2


35  

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:

不要试图剽窃@JosvicZammit,但是使用ContentType是错误的方法。这只是一个浪费的DB查询。您可以从_meta属性获得所需信息:

from django.core.urlresolvers 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,))

#3


7  

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

这与Josvic Zammit的代码片段得到了相同的结果,但是没有击中数据库:

from django.core import urlresolvers
from django.db import models

class MyModel(models.Model):

    def get_admin_url(self):
        return urlresolvers.reverse("admin:%s_%s_change" % (self._meta.app_label, self._meta.model_name), args=(self.id,))

#4


4  

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

就用这一行,也是python 3准备好了:

from django.core.urlresolvers 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.

更多关于这一点,请参见django管理站点doc,反向管理url。

#5


2  

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

因此,将Chris、Josvic和Josh的答案组合在一起,这是一个复制粘贴方法,您可以添加到模型中(在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,))

#1


27  

This Django snippet should do:

这个Django代码片段应该如下:

from django.core import urlresolvers
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 urlresolvers.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.

self指的是父模型类,即self。id指的是对象的实例id,您还可以将其设置为模型上的属性,方法签名的顶部粘贴@property decorator。

#2


35  

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:

不要试图剽窃@JosvicZammit,但是使用ContentType是错误的方法。这只是一个浪费的DB查询。您可以从_meta属性获得所需信息:

from django.core.urlresolvers 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,))

#3


7  

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

这与Josvic Zammit的代码片段得到了相同的结果,但是没有击中数据库:

from django.core import urlresolvers
from django.db import models

class MyModel(models.Model):

    def get_admin_url(self):
        return urlresolvers.reverse("admin:%s_%s_change" % (self._meta.app_label, self._meta.model_name), args=(self.id,))

#4


4  

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

就用这一行,也是python 3准备好了:

from django.core.urlresolvers 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.

更多关于这一点,请参见django管理站点doc,反向管理url。

#5


2  

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

因此,将Chris、Josvic和Josh的答案组合在一起,这是一个复制粘贴方法,您可以添加到模型中(在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,))