反向关系在django模型中不起作用?
我有以下疑问反向关系在django模型中不起作用?,django,django-models,Django,Django Models,我有以下疑问 SELECT DISTINCT P.intPartID FROM tbmstpart p, tbtrnappraisalquestion q WHERE p.intPartID = q.intPartID AND q.intTemplateID =4 我的模特是 class tbmstpart(models.Model): intPartID = models.AutoField(primary_key=True,db_column="intPartID") vchPartnam
SELECT DISTINCT P.intPartID FROM tbmstpart p, tbtrnappraisalquestion q WHERE p.intPartID = q.intPartID AND q.intTemplateID =4
我的模特是
class tbmstpart(models.Model):
intPartID = models.AutoField(primary_key=True,db_column="intPartID")
vchPartname = models.CharField("PartName", max_length=50,db_column="vchPartname")
def __unicode__(self):
return self.vchPartname
class Meta:
db_table = 'tbmstpart'
verbose_name = 'PartName'
verbose_name_plural = 'PartNames'
class tbmstsection(models.Model):
intSectionID = models.AutoField(primary_key=True,db_column="intSectionID")
vchSectionName = models.CharField("SectionName", max_length=50,db_column="vchSectionName")
def __unicode__(self):
return self.vchSectionName
class Meta:
db_table = 'tbmstsection'
verbose_name = 'SectionName'
verbose_name_plural = 'SectionNames'
class tbtrnappraisalquestion(models.Model):
STATUS = (
('1','Yes'),
('0','No')
)
intQuesID = models.AutoField(primary_key=True,db_column="intQuesID")
intTemplateID= models.ForeignKey(tbmsttemplate,verbose_name="Template",db_column="intTemplateID",related_name="tbmsttemplate_intTemplateID")
intSectionID= models.ForeignKey(tbmstsection,verbose_name="Section",db_column="intSectionID",related_name="tbtrnappraisalquestion_intSectionID")
intPartID= models.ForeignKey(tbmstpart,verbose_name="Part",db_column="intPartID",related_name="tbtrnappraisalquestion_intPartID")
txtQuestion = models.TextField("Question",db_column="txtQuestion")
#txtQuestion = RichTextField()
enumRating = models.CharField("Rating",max_length=5,choices=STATUS,db_column="enumRating")
enumComment = models.CharField("Comment",max_length=5,choices=STATUS,db_column="enumComment")
intOrder = models.CharField("Sequence",max_length=2,db_column="intOrder")
def __unicode__(self):
return self.txtQuestion
class Meta:
db_table = 'tbtrnappraisalquestion'
verbose_name = 'AppraisalQuestion'
verbose_name_plural = 'AppraisalQuestions'
我试过这样做
parts_list = tbmstpart.objects.filter(tbtrnappraisalquestion__intTemplateID__exact=4)
但这是一个错误
在/opas/list\u测试中出现字段错误/
无法将关键字“TBTrnapraisalQuestion”解析到字段中。选项包括:intPartID、TBTrnapraisalQuestion\u intPartID、vchPartname
提前感谢?您在
tbtrnappraisalquestion\uuuu intTemplateID
中输入了\uuuuuuuu
,而您想要的是tbtrnappraisalquestion\u intTemplateID
将您的查询更改为
假设您的tbmstmplate
模型将intTemplateID
作为int主键
parts_list = tbmstpart.objects.filter(tbtrnappraisalquestion_intPartID__intTemplateID__intTemplateID__exact=4)
#there is no typo here, you have to do __intTemplateID__intTemplateID as your pk field in `tbmsttemplate` and FK `tbtrnappraisalquestion` are same
正如@Rohan所说,我试过这个。工作正常
parts_list = tbmstpart.objects.filter(tbtrnappraisalquestion_intPartID__intTemplateID__exact=4).distinct()
我照你说的做了。但它抛出了一个错误,无法将关键字“tbtrnappraisalquestion\u intTemplateID”解析到字段中。选项有:intPartID、TBTrnapraisalQuestion\u intPartID、VCHPartName谢谢您的帮助。但是你能不能把tbtrnappraisalquestion\u intPartID\u intTemplateID\u intTemplateID\u精确=4。