0
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”类型的参数不可迭代提前谢谢

4

2 回答 2

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
于 2013-10-28T13:41:42.913 回答
0

我认为您可以使用django Q 对象大于小于来简化代码:

无需使用 raw 或 fororloop。

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

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
于 2013-10-28T13:42:34.293 回答