对于django模型,如何获取django管理员URL以添加另一个或列表对象等?

时间:2022-12-05 19:21:15

As much as I love the django documentation, the section on bookmarklets in the admin is strangely vague.

尽管我喜欢django文档,但管理员中关于bookmarklet的部分却很奇怪。

My question is this: If I'm in a view and I have a django model (or, in some cases, an actual object), how can I get to the relevant admin pages for that model (or object)? If I have the object coconut_transportation.swallow.objects.all()[34], how can I jump right to the admin page to edit that particular swallow?

我的问题是:如果我在视图中并且我有一个django模型(或者,在某些情况下,是一个实际的对象),我怎样才能到达该模型(或对象)的相关管理页面?如果我有对象coconut_transportation.swallow.objects.all()[34],我怎么能直接跳到管理页面来编辑那个特定的燕子?

Likewise, how can I get the URL for the admin page to add another swallow?

同样,如何获取管理页面的URL以添加另一个吞咽?

3 个解决方案

#1


47  

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

obj = coconut_transportation.swallow.objects.all()[34]

# list url
url = reverse("admin:coconut_transportation_swallow_changelist")

# change url
url = reverse("admin:coconut_transportation_swallow_change", args=[obj.id])

# add url
url = reverse("admin:coconut_transportation_swallow_add")

#2


10  

You can retrieve this from the actual object instance, this worked for me:

你可以从实际的对象实例中检索这个,这对我有用:

from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get_for_model(object.__class__)
object_admin_url = django.core.urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(object.pk,))

See this: http://djangosnippets.org/snippets/1916/

见:http://djangosnippets.org/snippets/1916/

#3


9  

You can actually retrieve the info without making a query to ContentTypes

您可以实际检索信息,而无需查询ContentTypes

Just add this to your model class:

只需将其添加到您的模型类:

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

#1


47  

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

obj = coconut_transportation.swallow.objects.all()[34]

# list url
url = reverse("admin:coconut_transportation_swallow_changelist")

# change url
url = reverse("admin:coconut_transportation_swallow_change", args=[obj.id])

# add url
url = reverse("admin:coconut_transportation_swallow_add")

#2


10  

You can retrieve this from the actual object instance, this worked for me:

你可以从实际的对象实例中检索这个,这对我有用:

from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get_for_model(object.__class__)
object_admin_url = django.core.urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(object.pk,))

See this: http://djangosnippets.org/snippets/1916/

见:http://djangosnippets.org/snippets/1916/

#3


9  

You can actually retrieve the info without making a query to ContentTypes

您可以实际检索信息,而无需查询ContentTypes

Just add this to your model class:

只需将其添加到您的模型类:

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