Can we execute a SQL like query using User defined Manager in Django?
Below is my model details
class QuesManager(db.Manager):
def with_counts(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute("""
开发者_C百科 SELECT Q.question, Q.title, Q.qid, V.up_qid, V.down_qid
FROM Question Q, Votes_ques V
WHERE Q.qid=V.qid
ORDER BY 3 DESC""")
result_list = []
for row in cursor.fetchall():
p = self.model(question=row[0], title=row[1], up_vote=row[2], down_vote=row[3])
result_list.append(p)
return result_list
class dummy(db.Model):
obj = QuesManager()
class Question(db.Model):
userid = db.CharField(max_length=50)
posted = db.DateTimeField(auto_now=True)
question = db.TextField(max_length=500)
qid = db.AutoField(primary_key=True)
title = db.TextField(max_length=80)
tags = db.ManyToManyField('Tags')
class Votes_ques(db.Model):
qid = db.ForeignKey('Question')
up_qid = db.IntegerField()
down_qid = db.IntegerField()
from mysite.answers.models import dummy
from mysite.answers.models import Votes_ques
from mysite.answers.models import Question
Does, Vote = dummy.obj.all()
will give all result set of the query inside the QuesManager class. Anyone have any idea on this...
Thanks!
Perhaps you're simply looking for this:
questions = Question.objects.all()
for question in questions:
votes = question.votes_ques_set.all()
精彩评论