Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form throws TypeError using get_form_kwargs within DFT but no errors passing kwargs outside it #197

Open
quinceleaf opened this issue Jun 30, 2021 · 0 comments

Comments

@quinceleaf
Copy link

quinceleaf commented Jun 30, 2021

Using django-formtools 2.3, Django 3.2

Issue: form used outside of DFT can be passed kwarg without issue, but same form throws TypeError when kwarg passed using get_form_kwargs within DFT

I am using DFT on a 2-part form for creating a bill-of-materials. For updating, I use standard UpdateViews with only the parts of the form user wants to modify. So I am able to see the forms/formsets working on their own, as well as when used by DFT.

My issue is when passing kwargs using get_form_kwargs to modify the queryset of one field:

  • when using my ModelForm by itself via UpdateView, I can pass form_kwargs without error and the form/querysets render as expected:
< in forms.py >

class BillOfMaterialsLineForm(common_forms.BaseModelForm):
    def __init__(self, *args, **kwargs):
        self.product = kwargs.pop("product", None)
        super(BillOfMaterialsLineForm, self).__init__(*args, **kwargs)

        if self.product:
            self.fields["item"].queryset = models.Item.objects.exclude(
                version_key=self.product.version_key
            )
            self.fields["item"].widget.queryset = models.Item.objects.exclude(
                version_key=self.product.version_key
            )

< in views.py >

        formset = self.form_class(
            instance=self.object,
            queryset=models.BillOfMaterialsLine.objects.filter(
                bill_of_materials=self.object
            ),
            prefix="lines",
            form_kwargs={"product": self.object.product},
        )
  • howver, when using the same form within DFT, passing the kwarg via get_form_kwargs, throws TypeError: __init__() got an unexpected keyword argument 'product' over the kwarg:
    def get_form_kwargs(self, step, **kwargs):
        kwargs = super(BillOfMaterialsWizardCreateView, self).get_form_kwargs(step)
        if step == "lines":
            kwargs["product"] = models.Product.objects.get(id=self.kwargs["pk"])
        return kwargs

Not sure if I'm doing something incorrectly or if this is an error in DFT. I believe my use of get_form_kwargs is structured correctly, but I've only been able to locate a single example so that may be the problem.

Any feedback would be welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant