inline_related_objects
[Edit, Feb 2008: fixed typo—changed extra_content to
extra_context]
Today's neat
Django trick: getting the
create_update generic views for a model to display entries
from a subsidiary model in-line—like the
admin interface
does when you add edit_inline=models.TABULAR.
The (oldforms) Form object that gets generated by the create_update
view already has most of the required gubbins inside it; we just need a few extra steps to get at it:
- In
urls.py, set anextra_contextargument for the view to be a dictionary that containsinline_related_objects:{'inline_related_objects': model._meta.get_followed_related_objects(None) } - In the template, include a loop to pull in all of the related objects:
{% for related_object in inline_related_objects %}{% edit_inline related_object %}{% endfor %}within the<form>tag. - Since the
edit_inlinetemplate tag is not a built-in Django tag, the template also needs to load up theadmin_modifyextension:{% load admin_modify %} - Make local copies of the relevant admin templates (such as
admin/edit_inline_tabular.htmlandwidget/foreign.html) and tweak appropriately.
Of course, I didn't figure this out until after I'd spent a couple of hours dismantling the admin interface code with a view to stealing the relevant bits. Roll on the Django book.