Question

I am using an inline formset and need to change the queryset of one of the non parent model's form fields when the formset is instantiated.

class Foo(Model):
   name = models.TextField()

class Bar(Model):
   foo = models.ForiegnKey(Foo)
   other_model = models.ForeignKey(OtherModel)

class BarForm(ModelForm):
   class Meta:
      model=Bar


foo = Foo.object.get(id=1)
FormSet = inlineformset_factory(Foo, Bar, form=BarForm)
formset = FormSet(instance=foo)

Depending on the value of foo which isn't determined until I enter the view code, I need to alter the queryset of the 'other_model' field in the BarForm for all forms in the formset. Is there a way to do this?

Was it helpful?

Solution

If I understand you correctly, this is what you can do... You can override BaseInlineFormSet, then manually set the query set for that field on every form in the formset.

So in your forms.py, you would do this:

class BaseBarFormSet(BaseInlineFormSet):
    def __init__(self, other_model_queryset, *args, **kwargs):
        super(BaseInlineFormSet, self).__init__(*args, **kwargs)

        for form in self.forms:
            form.fields['other_field'].queryset = other_model_queryset

Note how the first argument to __init__ is the queryset you want to set.

Then in you view, you would just modify your current code accordingly. Pass in that new BaseBarFormSet in the factory function:

FormSet = inlineformset_factory(Foo, Bar, form=BarForm, formset=forms.BaseBarFormSet) # notice formset=forms.BaseBarFormSet

Then pass the queryset you want for the other field to the actual FormSet class created by the factory function:

formset = FormSet(OtherModel.objects.filter(…), instance=foo) #notice the first parameter

Formsets are very complicated sometimes, so hopefully that made sense… let me know if you have problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top