简体   繁体   English

在Django中查询多个表

[英]Querying multiple tables in Django

I'm beginner in Django and Python, I started developing survey app. 我是Django和Python的初学者,我开始开发调查应用程序。 It's based on https://github.com/jessykate/django-survey I added few features, but I'm having problem with results page, to be more precisely how to get data to present them. 它基于https://github.com/jessykate/django-survey,我添加了一些功能,但是结果页面存在问题,更确切地说是如何获取数据以呈现它们。 Here's what models with most important fields look like: 以下是具有最重要字段的模型:

class Survey(models.Model):
    name = models.CharField(max_length=250)

class Question(models.Model):
    text = models.TextField()
    survey = models.ForeignKey(Survey)
    choices = models.TextField()

class Response(models.Model):
    survey = models.ForeignKey(Survey)

class AnswerBase(models.Model):
    question = models.ForeignKey(Question)
    response = models.ForeignKey(Response)  

class AnswerText(AnswerBase):
    body = models.TextField(blank=True, null=True)

class AnswerRadio(AnswerBase):
    body = models.TextField(blank=True, null=True)

    and few more Answer.. 

I think data in this format would be good to process later in js and display as bar char: 我认为这种格式的数据以后可以在js中处理并显示为bar char:

results = [{'some_question_text':
            [{'answer':'answer1','count': 11},{'answer':'answer2','count': 6}, ..]}
          ,..]

I could't came up how to do it in django way, so i tried in sql. 我无法提出如何以Django的方式进行操作,因此我尝试了sql。 Problem is, it works only with one answer type, when I add another condition like 'or ab.id==polls_answerselect.answerbase_ptr_id' query returns strange results. 问题是,当我添加另一种条件(如“或ab.id == polls_answerselect.answerbase_ptr_id”)时,查询仅返回一种奇怪的结果。

Here's what I've done: 这是我所做的:

cursor = connection.cursor()
cursor.execute("select q.text as qtext, ar.body as ans, ab.id as Aid, q.id as Qid, count(ar.body) as count \
            from polls_answerbase ab, polls_answerradio ar, polls_question q, polls_survey s \
            where ab.id==ar.answerbase_ptr_id \
            and ab.question_id==q.id \
            and s.id==q.survey_id \
            group by ar.body")
rows = dictfetchall(cursor)
result = {}
for r in rows: 
    res[r['qtext']] = []
    res[r['qtext']].append({'ans': r['ans'], 'count': r['count']})

What is better and correct way to solve my problem? 有什么更好和正确的方法来解决我的问题?

It looks like what you want here is a question list filtered by survey, and you want it in json format. 看起来您想要的是按调查筛选的问题列表,并且您希望它采用json格式。

Take a look at http://django-rest-framework.org/ It comes with a set of predefined class based views that support multiple response formats, json being one of them. 看看http://django-rest-framework.org/。它带有一组基于预定义类的视图,这些视图支持多种响应格式,其中json是其中一种。 The tutorial on that site walks you through setting it up, and uses simple tests along the way to verify you're doing it right. 该站点上的教程将引导您进行设置,并在使用过程中使用简单的测试来验证您的操作是否正确。 You can do something similar for your models. 您可以为模型做类似的事情。

I'm a Python/Django beginner as well and found it very easy to pick up. 我也是Python / Django的初学者,并且发现它很容易上手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM