0
에서 예기치 않은 키워드 인수를 가지고, 내가 반환 된 다음 얻을 : 여기 형식 오류 : __init의 __()가 <a href="https://docs.djangoproject.com/en/1.11/topics/forms/formsets/#passing-custom-parameters-to-formset-forms" rel="nofollow noreferrer">"Passing custom parameters to formset forms"</a>에 장고 문서를 다음에서 장고 양식 및 해당 formset
__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'
내가 문서와 패스를 따라 내 생각이다 formset form_kwargs에서 'choices'를 선택하십시오. 내가) (form.is_valid에서 다른 필드를 업데이트하려면이 필드를 사용되기 때문에 나는 forms.Form을 사용하고 & Formset-
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
내 양식.
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
)
이 충돌 다른 사람에 대한 해결책
의 kwargs.pop ('선택') 형태로 슈퍼() 이상 할 필요가 없었다. 초기화
이 오류를 해결했지만 ChoiceField에 데이터를로드하지 않고 formset을 표시했습니다. .. I합니다 (초기화 문제를 해결 변경 될 수있는 ModelChoiceField & 세트의 검색어 = 없음에 필드를 변경
감사 옌스, 그 오류를 해결 ! +1 이제 'old_transaction'필드에는 쿼리 세트 데이터가 없지만 또 다른 문제입니다. 도움을 위해 다시 한번 감사드립니다. – Emile