类型的参数' NoneType#39;在django表单中不可迭代

时间:2013-10-28 11:16:08

标签: django django-forms

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

如果查询中没有返回的行,则会引发异常 异常类型:TypeError 例外价值:
“NoneType”类型的参数不可迭代 提前致谢

2 个答案:

答案 0 :(得分:0)

如果res不是None,您只能迭代cleaned_data。此外,您需要返回self.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() 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

{{1}}

答案 1 :(得分:0)

我认为您可以使用django Q objectgreat than以及less than简化代码:

无需使用原料或foorloop。

我不知道你的模特,所以你查询对吗? (我的查询对吗?)

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