0
from django import forms
from django.db.models import Q
from django.core.exceptions import ValidationError
from schdeules.models import tbmsttemplate,tbmstreviewsched,tbtrnrevdepartments,tbtrnrevdesignations,tbmstappraisalsched,tbtrnappraisalreview,tbmstdepartment
class tbmstappraisalschedForm(forms.ModelForm):
    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

        qry = "SELECT intAppSchedID FROM tbMstAppraisalSched WHERE intDeptID ='"+depart_id+"' AND (('"+fromdate+"' BETWEEN  sdtFromDate AND  sdtToDate) OR ('"+todate+"' BETWEEN  sdtFromDate AND sdtToDate))"

        res = tbmstappraisalsched.objects.raw(qry)

        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
    class Meta:
        model = tbmstappraisalsched

上面的代码抛出一个错误:异常值:不能连接'str'和'tbmstdepartment'对象

提前致谢

4

1 回答 1

0

它抛出该错误是因为depart_id它是tbmstdepartment该类的对象;并且您正尝试在查询中将其连接起来。

改为这样做:

qry = """
   SELECT
        intAppSchedID
   FROM
        tbMstAppraisalSched
   WHERE
        intDeptID = '{0}'
   AND
        (('{1}' BETWEEN sdtFromDate AND sdtToDate)
   OR
        ('{2}' BETWEEN sdtFromDate and sdtToDate))
"""
res = tbmstappraisalsched.objects.raw(qry.format(depart_id.pk, fromdate, todate))
于 2013-10-28T10:21:11.447 回答