Skip to content Skip to sidebar Skip to footer

Add Extra Parameter To Inlineformset_factory

After a few hours of searching I must admit that I am defeated. I have read the Django docs but I really can't find a solution to my problem. Consider the following line of code: E

Solution 1:

I solved it by constructing this method

def set_extra_forms(extra_forms, **kwargs):
    EmploymentFormSet =inlineformset_factory(Profile, Employment, form=EmploymentForm, extra=extra_forms)
    returnEmploymentFormSet(**kwargs)

Now I do believe this is the way to go but I have to refactor the code in order to make it more dynamic right now its connected to one class and one class only but it works like a charm.

Solution 2:

Define the formset inside a view

defemployment_view(request, pk=None):
  #Define extra forms variable
  extra_forms = Employment.objects.filter(profile__pk=self.kwargs['pk']).count()
  #Define formset inside the view function
  EmploymentFormSet = inlineformset_factory(Profile, Employment, form=EmploymentForm, extra=extra_forms)
  #Use this in context
  context['emp_formset'] = EmploymentFormSet()

Post a Comment for "Add Extra Parameter To Inlineformset_factory"