2013-11-22 51 views
0

海,我有以下查詢反向關係船在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

在此先感謝?

回答

0

我想這一個@Rohan如說。它的做工精細

parts_list = tbmstpart.objects.filter(tbtrnappraisalquestion_intPartID__intTemplateID__exact=4).distinct() 
1

你有__tbtrnappraisalquestion__intTemplateID錯字,而你想tbtrnappraisalquestion_intTemplateID,單下劃線。

查詢更改爲

假設你tbmsttemplate模型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 
+0

我照你說。但它拋出一個錯誤無法解析關鍵字「tbtrnappraisalquestion_intTemplateID」到現場。選擇是:intPartID,tbtrnappraisalquestion_intPartID,vchPartname – ram

+0

謝謝你的工作。但是你可以吃嗎?tbtrnappraisalquestion_intPartID__intTemplateID__intTemplateID__exact = 4。 – ram

+0

@ user2627385,請參閱代碼中的註釋。 – Rohan