海,我有以下查詢反向關係船在django模型中不工作?
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)
但在拋出一個錯誤
FieldError/opas/list_questions_test/ 無法將關鍵字'tbtrnappraisalquestion'解析爲字段。選擇是:intPartID,tbtrnappraisalquestion_intPartID,vchPartname
在此先感謝?
我照你說。但它拋出一個錯誤無法解析關鍵字「tbtrnappraisalquestion_intTemplateID」到現場。選擇是:intPartID,tbtrnappraisalquestion_intPartID,vchPartname – ram
謝謝你的工作。但是你可以吃嗎?tbtrnappraisalquestion_intPartID__intTemplateID__intTemplateID__exact = 4。 – ram
@ user2627385,請參閱代碼中的註釋。 – Rohan