gpt4 book ai didi

django - 尝试更新模型对象时,类型 'NoneType' 的参数不可迭代

转载 作者:行者123 更新时间:2023-12-04 04:57:49 24 4
gpt4 key购买 nike

出现错误 Argument of type 'NoneType' is not iterable 并且无法找出原因。

它发生在行 if model_form.is_valid():

View .py

def update_model(request, pricemodel_id):
""" Update a part given its id. """
pricemod = Part.objects.get(id=pricemodel_id)
if request.method == "POST":
print "post request"
model_form = priceform(request.POST, request.FILES, instance=pricemod)
model_form.is_update = True
if request.user == pricemod.adder:
#from ipdb import set_trace; set_trace()
if model_form.is_valid():

表格.py

class priceform(ModelForm):
# price Form: form associated to the Part model
def __init__(self, *args, **kwargs):
super(priceform, self).__init__(*args,**kwargs)
self.is_update=False
choices = UniPart.objects.all().values('manufacturer').distinct()
def clean(self):
if self.cleaned_data and 'modelname' not in self.cleaned_data:
raise forms.ValidationError("Some error message")
if not self.is_update:
return self.cleaned_data
return self.cleaned_data
class Meta:
model = Part

最佳答案

看起来是因为您的 clean 方法中的缩进错误。目前它不返回任何东西,即它返回 None,但必须返回 self.cleaned_data。您所有的 clean 方法代码都在 if 下,这将引发异常。如果此 if 不匹配,则返回 None。

试试这个:

class priceform(ModelForm):
# ...
def clean(self):
if self.cleaned_data and 'modelname' not in self.cleaned_data:
raise forms.ValidationError("Some error message")
if not self.is_update:
return self.cleaned_data
return self.cleaned_data

# ...

关于django - 尝试更新模型对象时,类型 'NoneType' 的参数不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16548557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com