在django管理员中显示链接到对象的完整更改表单 [英] Display link to full change form for object in django admin
问题描述
这是我在做什么:
我有一个包含名称,地址等的管理员模型。
我有一个属性模型,内置了内容,与管理器模型相关。
我希望经理模型能够在更改表单中显示与其相关的每个属性的链接。
这是可以做的吗?
当然可以覆盖您的更改视图。
http:// docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
将模板从真正的管理模板目录,并将其放在任何你喜欢的地方(因为你可以用 change_form_template
指出它,并添加一些额外的东西来显示。
我经常这样做。
class MyModelAdmin(admin.ModelAdmin):
change_form_template ='myapp / new_change_form.html'
def change_view(self,request,object_id,extra_context = None):
properties = Manager.objects.get(id = object_id).property_set.all ()
extra_context = {'properties':properties}
super(MyModelAdmin,self).change_view(request,object_id,extra_context)
在管理模板中找到一个地方添加你自己的一些HTML。
< ul>
{%属性%}
< li>
< a href ={%url admin:myapp_manager_change property.id%}>编辑{{property}}< / a>
< / li>
{%endfor%}
< / ul>
Because nested inlines are not yet supported, I am wondering how to display a link to the related object as an alternative. I came across a question the other day with a link to an app, but I can't seem to find it again.
Here is what I am looking at doing:
I have a manager model that contains name, address etc.
I have a property model that has inlines and is related to the manager model.
I am wanting the manager model to be able to display a link in the change form for each of the properties that are related to it.
Is this something that can be done?
Sure, you can just overwrite your change view. http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
Copy the template from the real admin template directory and place it anywhere you like (since you can point at it with change_form_template
, and add in some extra stuff to display.
I do this pretty commonly.
class MyModelAdmin(admin.ModelAdmin):
change_form_template = 'myapp/new_change_form.html'
def change_view(self, request, object_id, extra_context=None):
properties = Manager.objects.get(id=object_id).property_set.all()
extra_context = { 'properties':properties }
super(MyModelAdmin, self).change_view(request, object_id, extra_context)
Find a place in the admin template to add some of your own HTML.
<ul>
{% for property in properties %}
<li>
<a href="{% url admin:myapp_manager_change property.id %}">Edit {{ property }}</a>
</li>
{% endfor %}
</ul>
这篇关于在django管理员中显示链接到对象的完整更改表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!