Django咖喱错误

时间:2012-01-08 22:44:08

标签: python django django-forms

我试图将一个参数asins传递给一个modelformset中的每个表单,所以我按照这里描述的解决方案https://stackoverflow.com/a/624013/389453,继承代码

VariationFormSet = modelformset_factory(Variation, form=VariationASINMatch, extra=0)
VariationFormSet.form = staticmethod(curry(VariationFormSet, asins=extra_asins))
formset = VariationFormSet(request.POST or None, request.FILES or None, queryset=variation_q)

表示形式:

class VariationASINMatch(forms.ModelForm):

    def __init__(self, asins, *args, **kwargs):
        instance = kwargs['instance']
        super(VariationASINMatch, self).__init__(*args, **kwargs)
        self.fields['asin'] = fields.ChoiceField(choices=asins[instance.upc])


    class Meta:
        model = Variation
        fields = ('child_sku', 'upc', 'asin', 'id_type')
不幸的是,由于某种原因,咖喱导致了这种错误:

Traceback:
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  307.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  197.             return view(request, *args, **kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  28.             return bound_func(*args, **kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  24.                 return func(self, *args2, **kwargs2)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1079.                 response = self.response_action(request, queryset=cl.get_query_set())
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/contrib/admin/options.py" in response_action
  836.             response = func(self, request, queryset)
File "/home/paulo/Documents/Projects/tsn_env/tsn/../tsn/product/admin.py" in get_variation_asins
  130.             formset = VariationFormSet(request.POST or None, request.FILES or None, queryset=variation_q)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/forms/models.py" in __init__
  415.         super(BaseModelFormSet, self).__init__(**defaults)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/forms/formsets.py" in __init__
  47.         self._construct_forms()
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/forms/formsets.py" in _construct_forms
  108.             self.forms.append(self._construct_form(i))
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/forms/models.py" in _construct_form
  443.         return super(BaseModelFormSet, self)._construct_form(i, **kwargs)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/forms/formsets.py" in _construct_form
  127.         form = self.form(**defaults)
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/utils/functional.py" in _curried
  55.         return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "/home/paulo/Documents/Projects/tsn_env/lib/python2.7/site-packages/django/forms/models.py" in __init__
  415.         super(BaseModelFormSet, self).__init__(**defaults)

Exception Type: TypeError at /admin/product/product/
Exception Value: __init__() got an unexpected keyword argument 'instance'

我已经找到了解决方案,但到目前为止没有找到,我欢迎任何解决方案/建议。感谢

1 个答案:

答案 0 :(得分:2)

所以我终于解决了这个问题,错误就在这里:

VariationFormSet = modelformset_factory(Variation, form=VariationASINMatch, extra=0)
VariationFormSet.form = staticmethod(curry(VariationFormSet, asins=extra_asins))

而不是给curry Form类VariationASINMatch,我给它了formset类VariationFormSet

所以看起来应该是这样的

VariationFormSet = modelformset_factory(Variation, form=VariationASINMatch, extra=0)
VariationFormSet.form = staticmethod(curry(VariationASINMatch, asins=extra_asins))