Display link to full change form for object in django admin
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 a开发者_如何学运维re 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>
精彩评论