且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在django管理员中显示链接到对象的完整更改表单

更新时间:2023-02-08 19:27:21

当然可以覆盖您的更改视图。
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>