类型为'的参数;非类型';在django froms中不可使用
若查询中并没有返回行,那个么它将引发异常 异常类型:TypeError 异常值:类型为'的参数;非类型';在django froms中不可使用,django,django-forms,Django,Django Forms,若查询中并没有返回行,那个么它将引发异常 异常类型:TypeError 异常值: 类型为“NoneType”的参数不可iterable 提前感谢如果不是无,您只能迭代res。此外,您需要返回cleaned_数据,而不是self.cleaned_数据 def clean(self): """ Override the default clean method to check whether this course has been already inputted.
类型为“NoneType”的参数不可iterable
提前感谢如果不是
无
,您只能迭代res
。此外,您需要返回cleaned_数据
,而不是self.cleaned_数据
def clean(self):
"""
Override the default clean method to check whether this course has been already inputted.
"""
cleaned_data = super(tbmstappraisalschedForm, self).clean()
#appsched_id = str(self.cleaned_data.get('intAppSchedID'))
depart_id = self.cleaned_data.get('intDeptID')
fromdate = str(self.cleaned_data.get('sdtFromDate'))
todate = str(self.cleaned_data.get('todate'))
pk=self.instance.pk
#if tbmstappraisalsched.objects.filter(intDeptID=depart_id).exclude(pk=self.instance.pk).exists():
#qry = "SELECT intAppSchedID FROM tbMstAppraisalSched WHERE intDeptID ='"+depart_id+"' AND (('"+fromdate+"' BETWEEN sdtFromDate AND sdtToDate) OR ('"+todate+"' BETWEEN sdtFromDate AND sdtToDate))"
qry = """SELECT intAppSchedID FROM tbMstAppraisalSched WHERE intDeptID = '{}' AND (('{}' BETWEEN sdtFromDate AND sdtToDate) OR ('{}' BETWEEN sdtFromDate and sdtToDate))"""
res = tbmstappraisalsched.objects.raw(qry.format(depart_id.pk, fromdate, todate))
for re in res:
if(re.intAppSchedID != pk):
msg = "The slot for selected department and selected dates exists"
raise ValidationError(msg)
else:
return self.cleaned_data
我认为您可以使用和简化代码: 不需要使用raw或foorloop 我不知道你是模特,你问得对吗?(我的问题对吗?)
在哪一行?如果它在
for
循环中,那么res
的值是多少?@AugustoMen它在for循环中提升为什么你有tbmstaapprovalsched
类?你为什么不用django ORM?
def clean(self):
"""
Override the default clean method to check whether this course has
been already inputted.
"""
cleaned_data = super(tbmstappraisalschedForm, self).clean()
depart_id = self.cleaned_data.get('intDeptID')
fromdate = str(self.cleaned_data.get('sdtFromDate'))
todate = str(self.cleaned_data.get('todate'))
pk = self.instance.pk
qry = """SELECT intAppSchedID FROM tbMstAppraisalSched
WHERE intDeptID = '{}' AND (('{}' BETWEEN sdtFromDate AND sdtToDate)
OR ('{}' BETWEEN sdtFromDate and sdtToDate))"""
res = tbmstappraisalsched.objects.raw(qry.format(depart_id.pk, fromdate,
todate))
if res:
for re in res:
if re.intAppSchedID != pk:
msg = "The slot for selected department and selected dates exists"
raise ValidationError(msg)
return cleaned_data
def clean(self):
"""
Override the default clean method to check whether this course has been already inputted.
"""
cleaned_data = super(tbmstappraisalschedForm, self).clean()
#appsched_id = str(self.cleaned_data.get('intAppSchedID'))
depart_id = self.cleaned_data.get('intDeptID')
fromdate = str(self.cleaned_data.get('sdtFromDate'))
todate = str(self.cleaned_data.get('todate'))
pk=self.instance.pk
#if
res = tbmstappraisalsched.objects.filter(
Q(sdtFromDate__lt=fromdate,sdtToDate__gt=fromdate) | \
Q(sdtFromDate__lt=todate,sdtToDate__gt=todate), ~Q(intAppSchedID=pk),
intDeptID=depart_id.pk,
)
if res.exists():
msg = "The slot for selected department and selected dates exists"
raise ValidationError(msg)
else:
return self.cleaned_data