Reputation: 891
I have the following 3 models, each of which has a 1-many relationship with it's children
* Experiment
* Site (lab, schools, etc.)
* Participants
Since a Site might have hundreds of participants, I've overridden it's models change_form.html, where I've added a 'bulk create' form in the 'after_field_sets' block.
Here's my my-project/templates/admin/my-app/site/change_form.html:
{% extends "admin/change_form.html" %}
{% load i18n admin_urls %}
{% block after_field_sets %}
{{ block.super }}
<h3>Bulk Create Participants</h3>
<form method="POST">
<input type="hidden" name="action" value="create"/>
<label>First uid (inclusive) <input type="text" name="firstUid"/></label>
<label>Last uid (inclusive) <input type="text" name="lastUid"/></label>
<label>Condition Order<input type="text" name="conditionOrder"/></label>
</form>
<hr/>
{% endblock %}
If I do nothing else, everything gets displayed properly, however without a custom view, I don't have any way of processing the custom form. When I add a get_urls()
and a custom view to my SiteAdmin
, which should allow me to process the custom form, only the 'after_field_sets' block is displayed.
Here's my Site ModelAdmin:
class SiteAdmin(admin.ModelAdmin):
fields = ['experiment', 'name', 'description']
readonly_fields = ['experiment']
inlines = [ParticipantInline]
def get_urls(self):
urls = super(SiteAdmin, self).get_urls()
my_urls = [
url(r"^(?P<pk>[0-9]+)/$", self.admin_site.admin_view(self.my_view,
cacheable=True)),
]
return my_urls + urls
def my_view(self, request):
context = dict(
self.admin_site.each_context(request),
opts = Site._meta,
change = True,
is_popup=False,
save_as=False,
has_delete_permission=False,
has_add_permission=False,
has_change_permission=False
)
if request.method == 'POST':
action = request.POST["action"]
firstUID = request.POST["firstUid"]
lastUID = request.POST["lastUid"]
if "create" == action:
for uid in range(firstUID, lastUID+1):
Participant.objects.create(site=site, uid=uid)
return TemplateResponse(request, "admin/experimentAdmin/site/change_form.html", context)
Is there more information I need to pass to the context to get the entire form to display?
Any help would be appreciated.
Upvotes: 1
Views: 1063
Reputation: 891
It turned out that a better way to do this was to not to override the change_form.html template, but rather create a ModelForm
for my Site model, where I defined some extra fields (not part of the actual model), set required=False on these fields, added them to the ModelAdmin's fieldset, and handled the optional extra form fields in my SiteAdmin::save_model()
class SiteForm(forms.ModelForm):
first_uid = forms.IntegerField(label='First UID', required=False)
last_uid = forms.IntegerField(label='Last UID', required=False)
conditions_order = forms.CharField(label='Conditions Order', required=False)
class Meta:
model = Site
fields = ("name", "description",
"first_uid", "last_uid", "conditions_order")
class SiteAdmin(admin.ModelAdmin):
form = SiteForm
inlines = [ParticipantInline]
fieldsets = (
(None, {
"fields": ("name", "description")
}),
("Bulk Create Participants", {
"fields": ("first_uid", "last_uid",
"conditions_order")
})
)
def save_model(self, request, obj, form, change):
# deal with optional fields if present
...
obj.save()
Upvotes: 1