I spent a fair amount of time trying to come up with a solution that I could re-use across sites. James' post contained the key piece of wisdom of extending BaseInlineFormSet
but strategically invoking calls against BaseFormSet
.
The solution below is broken into two pieces: a AdminInline
and a BaseInlineFormSet
.
- The
InlineAdmin
dynamically generates an initial value based on the exposed request object.
- It uses currying to expose the initial values to a custom
BaseInlineFormSet
through keyword arguments passed to the constructor.
- The
BaseInlineFormSet
constructor pops the initial values off the list of keyword arguments and constructs normally.
- The last piece is overriding the form construction process by changing the maximum total number of forms and using the
BaseFormSet._construct_form
and BaseFormSet._construct_forms
methods
Here are some concrete snippets using the OP's classes. I've tested this against Django 1.2.3. I highly recommend keeping the formset and admin documentation handy while developing.
admin.py
from django.utils.functional import curry
from django.contrib import admin
from example_app.forms import *
from example_app.models import *
class AttendanceInline(admin.TabularInline):
model = Attendance
formset = AttendanceFormSet
extra = 5
def get_formset(self, request, obj=None, **kwargs):
"""
Pre-populating formset using GET params
"""
initial = []
if request.method == "GET":
#
# Populate initial based on request
#
initial.append({
'foo': 'bar',
})
formset = super(AttendanceInline, self).get_formset(request, obj, **kwargs)
formset.__init__ = curry(formset.__init__, initial=initial)
return formset
forms.py
from django.forms import formsets
from django.forms.models import BaseInlineFormSet
class BaseAttendanceFormSet(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
"""
Grabs the curried initial values and stores them into a 'private'
variable. Note: the use of self.__initial is important, using
self.initial or self._initial will be erased by a parent class
"""
self.__initial = kwargs.pop('initial', [])
super(BaseAttendanceFormSet, self).__init__(*args, **kwargs)
def total_form_count(self):
return len(self.__initial) + self.extra
def _construct_forms(self):
return formsets.BaseFormSet._construct_forms(self)
def _construct_form(self, i, **kwargs):
if self.__initial:
try:
kwargs['initial'] = self.__initial[i]
except IndexError:
pass
return formsets.BaseFormSet._construct_form(self, i, **kwargs)
AttendanceFormSet = formsets.formset_factory(AttendanceForm, formset=BaseAttendanceFormSet)