Django formset - 空kwargs [英] Django formset - empty kwargs

查看:157
本文介绍了Django formset - 空kwargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用加法参数初始化一个Django格式集,以传递表单中的表单。但是,当我使用formset factory初始化formset并将变量传递给它时。 Kwargs总是空的

I am trying to initialize a Django formset with an addition parameter to pass on the forms in the formset. However when i initialize the formset with formset factory and pass the variable to it. the Kwargs is always empty

forms.py

from django.forms.formsets import BaseFormSet
from django.forms.formsets import formset_factory

class BaseCIFormSet(BaseFormSet):

    def __init__(self, *args, **kwargs):
        print kwargs
        self.grn_id = kwargs.pop('grn_id', None)
        super(BaseCIFormSet, self).__init__(*args, **kwargs)

    def _construct_forms(self):
        self.forms = []
        for i in xrange(self.total_form_count()):
            self.forms.append(self._construct_form(i, grn_id=self.grn_id))

Views.py

data['form-TOTAL_FORMS'] = count
data['form-INITIAL_FORMS'] = count
data['form-MAX_NUM_FORMS'] = ''

CIFormSet = formset_factory(CIForm, formset=BaseCIFormSet, extra=(count), can_delete=True)
formset = CIFormSet(data, grn_id)

打印kwargs总是返回和空的dict {},kwargs.pop总是默认。我做错了吗?

The "print kwargs" always returns and empty dict {} and the kwargs.pop always goes into default. Am i doing something wrong?

推荐答案

不,你没有。您传递的参数不是命名参数,因此存储在 * args 指针中。要让他们进入 ** kwargs dict你必须这样做

No, you don't. The parameters you pass are not named parameters, and are therefore stored in the *argspointer. To get them into the **kwargs dict you'd have to do this

formset = CIFormSet(data=data,grn_id=grn_id)

这篇关于Django formset - 空kwargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆