__init__() 得到了一个意外的关键字参数“instance”,其中包含用于 inlineformset_factory 的自定义表单集

Posted

技术标签:

【中文标题】__init__() 得到了一个意外的关键字参数“instance”,其中包含用于 inlineformset_factory 的自定义表单集【英文标题】:__init__() got an unexpected keyword argument 'instance' with custom formset for inlineformset_factory 【发布时间】:2017-12-31 01:28:59 【问题描述】:

我收到了这个错误

__init__() got an unexpected keyword argument 'instance'

每当我尝试发送带有为 inlineformset_factory 指定的 formset 参数的表单时。 任何想法为什么会发生这种情况?

这是我的代码。抱歉,没有评论我正在重写它很多试图了解 django 的工作原理

表格:

class PurchaseForm(forms.ModelForm):
    class Meta:
        model = Purchase
        fields = ['product', 'price', 'consumer']
        widgets = 
            'product': forms.Select(attrs = 
                'class': 'form-control input-sm',
            ),
            'consumer': forms.Select(attrs = 
                'class': 'form-control input-sm',
            ),
            'price': forms.NumberInput(attrs = 
                'class': 'form-control input-sm',
                'placeholder': 'Цена',
            ),
        
    discount = forms.DecimalField(
        min_value      = 0,
        decimal_places = 2,
        # required       = False,
        widget         = forms.NumberInput(
            attrs = 
                'class': 'form-control input-sm',
                'placeholder': 'Скидка'
            
        ),
    )


class PurchaseFormSet(BaseModelFormSet):
    def add_fields(self, form, index):
        super(PurchaseFormSet, self).add_fields(form, index)
        form.fields[DELETION_FIELD_NAME].label = ''

视图:

def receipt(request):
    purchase_formset = inlineformset_factory(
        Receipt,
        Purchase,
        form    = PurchaseForm,
        formset = PurchaseFormSet, <-- when I comment this everything works fine
    )

    if request.method == 'POST':
        receipt_form = ReceiptForm(
            request.POST,
            prefix = 'receipt'
        )
        if receipt_form.is_valid():
            receipt = receipt_form.save()
            receipt_form = ReceiptForm(
                instance = receipt,
                prefix   = 'receipt'
            )
            purchases_form = purchase_formset(
                request.POST,
                prefix   = 'purchase',
                instance = receipt <-- line that causes error according to traceback
            )
            if purchases_form.is_valid():
                purchases_form.save()
                return redirect('spendings:receipt')
    else:
        receipt_form = ReceiptForm(
            instance = Receipt(),
            prefix   = 'receipt'
        )
        purchases_form = purchase_formset(
            prefix = 'purchase'
        )

    context = 
        'receipt_form': receipt_form,
        'purchases_form': purchases_form
    
    return render(request, 'spendings/receipt/index.html', context)

【问题讨论】:

【参考方案1】:

如果你使用inlineformset_factory,那么你的表单集类应该继承BaseInlineFormSet

from django.forms import BaseInlineFormSet

class PurchaseFormSet(BaseInlineFormSet):
    ...

【讨论】:

以上是关于__init__() 得到了一个意外的关键字参数“instance”,其中包含用于 inlineformset_factory 的自定义表单集的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: __init__() 得到了一个意外的关键字参数 '__no_builder' Kivy

TypeError: __init__() 得到了一个意外的关键字参数“重新引用”

TypeError: __init__() 得到了一个意外的关键字参数“编码”

Django __init__() 得到了一个意外的关键字参数“用户”

__init__() 在 python/django 中得到了一个意外的关键字参数“mime”

TypeError:__init__() 得到了一个意外的关键字参数“categorical_features”:onehotencoder