Django 无法连接';str';和';TBMST部门';物体
上述代码引发了一个错误: 异常值:无法连接“str”和“tbmstdepartment”对象Django 无法连接';str';和';TBMST部门';物体,django,django-forms,Django,Django Forms,上述代码引发了一个错误: 异常值:无法连接“str”和“tbmstdepartment”对象 提前感谢它抛出了这个错误,因为department\u id是tbmstdepartment类的对象;您正试图在查询中连接它 改为这样做: from django import forms from django.db.models import Q from django.core.exceptions import ValidationError from schdeules.models impo
提前感谢它抛出了这个错误,因为
department\u id
是tbmstdepartment
类的对象;您正试图在查询中连接它
改为这样做:
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
使用原始SQL有什么特别的原因吗?我们可以轻松编写复杂的查询。我更不熟悉django sand python。所以我使用原始查询。python的哪个版本?如果它是2的旧分支,请尝试更新版本。您好,它正在工作。但若并没有从查询返回行,那个么它将引发一个类型错误,即;类型为“NoneType”的参数不可iterable。这是另一个单独的问题。你需要问一个新问题。
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))