Ramakrishna
Ramakrishna

Reputation: 301

reverse relation ship is not working in django models?

hai i have the following query

SELECT DISTINCT P.intPartID FROM tbmstpart p, tbtrnappraisalquestion q WHERE p.intPartID = q.intPartID AND q.intTemplateID =4

my models are

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'

i tried in this way

parts_list = tbmstpart.objects.filter(tbtrnappraisalquestion__intTemplateID__exact=4)

but it is throwing an error

FieldError at /opas/list_questions_test/ Cannot resolve keyword 'tbtrnappraisalquestion' into field. Choices are: intPartID, tbtrnappraisalquestion_intPartID, vchPartname

thanks in advance?

Upvotes: 0

Views: 57

Answers (2)

Ramakrishna
Ramakrishna

Reputation: 301

i tried this one as said by @Rohan. its working fine

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

Upvotes: 0

Rohan
Rohan

Reputation: 53316

You have typo of __ in tbtrnappraisalquestion__intTemplateID rather you want tbtrnappraisalquestion_intTemplateID, with single underscore.

Change your query to

Assuming your tbmsttemplate model has intTemplateID as int primary key,

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

Upvotes: 1

Related Questions