简体   繁体   English

django表单save()得到了一个意外的关键字参数?

[英]django forms save() got an unexpected keyword argument?

In my view: 在我看来:

image = simple_image_form.save(quiet=True)

my form: 我的表格:

class SimpleImageForm(forms.ModelForm):

    class Meta:
        model = Image
        fields = ("image",)

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request", None)
        super(SimpleImageForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        kwargs["commit"]=False
        obj = super(SimpleImageForm, self).save(*args, **kwargs)
        if self.request and self.request.user:
            obj.member = self.request.user.get_current_member()
            obj.save(*args, **kwargs)
        return obj

when I save, I get the error: 当我保存时,我收到错误:

save() got an unexpected keyword argument 'quiet'

I thought by placing **kwargs as a parameter, this wouldn't happen. 我认为将** kwargs作为参数,这不会发生。 What am I doing wrong? 我究竟做错了什么?

Note: quiet is a parameter in the obj.save function used in condition checks, but is neither an attribute of the form or the obj itself 注意:quiet是条件检查中使用的obj.save函数中的参数,但既不是表单的属性,也不是obj本身的属性

Yes, your override may handle the kwarg but you shouldn't do the same with the super call: 是的,你的覆盖可以处理kwarg但你不应该对super调用做同样的事情:

obj = super(SimpleImageForm, self).save(commit=False)

That should do. 那应该做。 This happens because the ModelForm 's save method signature is: 这是因为ModelFormsave方法签名是:

def save(self, commit=True):

So it doesn't match the way you're calling it. 所以它与你调用它的方式不符。

Hope this helps! 希望这可以帮助!

You need to have a field called quiet in order to save a value to that field. 您需要有一个名为quiet的字段才能将值保存到该字段。 I don't see any reference to such a field in the form, so I'm guessing there isn't one in the model. 我没有在表格中看到任何对这样一个字段的引用,所以我猜测模型中没有一个字段。 Even if there is, you probably have to say reference it in the form since you're defining what parts of the model you're working with. 即使有,你可能不得不说在表单中引用它,因为你正在定义你正在使用的模型的哪些部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM