I am trying to use inlineformset_factory to generate a formset. My models are defined as:
class Measurement(models.Model):
subject = models.ForeignKey(Subject)
experiment = models.ForeignKey(Experiment)
assay = models.ForeignKey(Assay)
values = models.CommaSeparatedIntegerField(blank=True, null=True)
class Experiment(models.Model):
date = models.DateField()
notes = models.TextField(max_length = 500, blank=True)
subjects= models.ManyToManyField(Subject)
in my view i have:
def add_measurement(request, experiment_id):
experiment = get_object_or_404(Experiment, pk=experiment_id)
MeasurementFormSet = inlineformset_factory(Experiment, Measurement, extra=10, exclude=('experiment'))
if request.method == 'POST':
formset = MeasurementFormSet(request.POST,instance=experiment)
if formset.is_valid():
formset.save()
return HttpResponseRedirect( experiment.get_absolute_url() )
else:
formset = MeasurementFormSet(instance=experiment)
return render_to_response("data_entry_form.html", {"formset": formset, "experiment": experiment }, context_instance=RequestContext(request))
but i want to restrict the Measurement.subject field to only subjects defined in the Experiment.subjects queryset. I have tried a couple of different ways of doing this but I am a little unsure what the best way to accomplish this is. I tried to over-ride the BaseInlineFormset class with a new queryset, but couldnt figure out how to correctly pass the experiment parameter.
Updated answer (I also included the information from here as a way to pass the parameter to the formset link):
views.py
def add_measurement(request, experiment_id):
experiment = get_object_or_404(Experiment, pk=experiment_id)
MeasurementFormSet = inlineformset_factory(Experiment, Measurement, extra=10, can_delete=True, form=MeasurementForm)
MeasurementFormSet.form = staticmethod(curry(MeasurementForm, experiment=experiment))
if request.method == 'POST':
formset = MeasurementFormSet(request.POST)
if formset.is_valid():
formset.save()
return HttpResponseRedirect( experiment.get_absolute_url() )
else:
formset = MeasurementFormSet()
return render_to_response("data_entry_form.html", {"formset": formset, "experiment": experiment }, context_instance=RequestContext(request))
forms.py
class MeasurementForm(ModelForm):
class Meta:
model = Measurement
def __init__(self, *args, **kwargs):
experiment = kwargs.pop('experiment')
super(MeasurementForm, self).__init__(*args, **kwargs)
self.fields["subject"].queryset = Subject.objects.filter(experiment=experiment)