Inline formet factory - submit request to child form

I came across quite complex games:

I need to inlineformset_factoryconnect mine ParentEntityto my external key binding ChildEntities.

My ChildEntitycontains a foreign key relationship that I need to filter out for login, so I need a request in ChildForm.

What I have tried so far:

  • I tried using form=kwarg, but I cannot pass an instance - just a class. Therefore, I do not need to add a request here.
  • I tried using formset=kwarg, but when I try to pass request=requestas kwarg inlineformset_factory, I get an error (Unexpected kwarg)

Any idea what I can do?

Thank! Ron

+2
source share
1 answer

Sometimes asking a colleague is even faster than StackOverflow:)

Here is my solution:

forms.py

class BaseFormSet(BaseInlineFormSet):

def __init__(self, *args, **kwargs):

    self.request = kwargs.pop("request")

    super(BaseFormSet, self).__init__(*args, **kwargs)

views.py

MyFormSet = inlineformset_factory(ParentEntity, ChildEntity, formset=BaseFormSet, form=ChildForm, extra=2, max_num=max_num, can_delete=False)
...
formset = MyFormSet(request.POST, instance=obj, request=request)
+3
source

Source: https://habr.com/ru/post/1629438/


All Articles