In following the Django docs on "Passing custom parameters to formset forms", I get the following returned:
__init__() got an unexpected keyword argument 'choices'
File "/Users/emilepetrone/Envs/kishikoi/lib/python3.6/site-packages/django/utils/functional.py" in __get__
35. res = instance.__dict__[self.name] = self.func(instance)
File "/Users/emilepetrone/Envs/kishikoi/lib/python3.6/site-packages/django/forms/formsets.py" in forms
144. for i in range(self.total_form_count())]
File "/Users/emilepetrone/Envs/kishikoi/lib/python3.6/site-packages/django/forms/formsets.py" in <listcomp>
144. for i in range(self.total_form_count())]
File "/Users/emilepetrone/Envs/kishikoi/lib/python3.6/site-packages/django/forms/formsets.py" in _construct_form
182. form = self.form(**defaults)
File "/Users/emilepetrone/Sites/kishikoi/kishikoi/transactions/forms.py" in __init__
119. super(SoldTransactionForm, self).__init__(*args, **kwargs)
Exception Type: TypeError at /transactions/create/sell/transactions/31tmhqsplg41jc8c/
Exception Value: __init__() got an unexpected keyword argument 'choices'
Here is my view where I follow the documentation and pass 'choices' in the formset form_kwargs.
class SellTransactionsView(LoginRequiredMixin, SetHeadlineMixin, UpdateView):
model = Transaction
template_name = "transactions/soldtransaction_form.html"
headline = "Sell Transaction"
fields = ['num_shares']
def get_object(self):
return Transaction.objects.get(
user=self.request.user,
identifier=self.kwargs['identifier']
)
def get_choices(self):
transaction = self.get_object()
choices = Transaction.objects.filter(
user=transaction.user,
).exclude(identifier=transaction.identifier)
return choices
def get_context_data(self, *args, **kwargs):
context = super(SellTransactionsView, self).get_context_data(
*args, **kwargs)
choices = self.get_choices()
formset = SoldFormset(form_kwargs={'choices': choices})
context.update({
"formset": formset,
})
return context
My Form & Formset- I'm using a forms.Form because I will be using these fields to update a different field in form.is_valid().
class SoldTransactionForm(forms.Form):
old_transaction = forms.ChoiceField()
num_shares = forms.IntegerField(
min_value=0
)
class Meta:
fields = [
'old_transaction',
'num_shares',
]
def __init__(self, *args, **kwargs):
super(SoldTransactionForm, self).__init__(*args, **kwargs)
self.fields['old_transaction'].queryset = kwargs.pop('choices')
SoldFormset = forms.formset_factory(
SoldTransactionForm,
formset=forms.BaseFormSet,
extra=2
)
SOLUTION
For others that hit this, the kwargs.pop('choices') did need to be above the Super() in the form.init
This resolved the error, but then presented a formset without loading the data in the ChoiceField. I changed the field to a ModelChoiceField & set queryset=None (to be changed in the init. That resolved the issues.
Pop before you call super:
def __init__(self, *args, **kwargs):
queryset = kwargs.pop('choices')
super(SoldTransactionForm, self).__init__(*args, **kwargs)
self.fields['old_transaction'].queryset = queryset
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With