Sometimes the related objects needs that the main object exists in order to edit and save them properly. There are two main solutions: override the ModelAdmin.add_view() view or remove the inlines only from the add view page (and not from the change page). The former requires a lot of coding, the latter it's impossible without patching Django because the inlines are not dynamic.
This simple solution hides the inline formsets only from the add page, and not from the change page. Adding an "if" structure it is possible to choose the inlines to use.
Example use case: when a related inline model have to save a file to a path that needs the ID key of the main model, this solution prevent the user to use the related inline model until the model it's saved.
Tested on Django-1.4, should work since Django-1.2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | admin.py (a slice)
==================
class PhotoInline(admin.TabularInline):
# ...
class GalleryAdmin(admin.ModelAdmin):
inlines = [PhotoInline]
add_form_template = 'admin/add_without_inlines.html'
# ...
templates/admin/add_without_inlines.html (full)
===============================================
{% extends "admin/change_form.html" %}
{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}
{{ inline_admin_formset.formset.management_form }}
{% endfor %}
{% endblock %}
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Another trick is to control this from Admin class instead of the template, by only setting the inline in the change_view
#
Please login first before commenting.