无法连接'str'和'tbmstdepartment'对象

时间:2013-10-28 10:11:27

标签: django django-forms

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'对象

提前致谢

1 个答案:

答案 0 :(得分:0)

它抛出该错误,因为depart_idtbmstdepartment类的对象;并且您正试图在查询中连接它。

请改为:

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))