开始Django:执行子选择和其他复杂查询 [英] Beginning Django: performing sub-selects and other complex queries

查看:113
本文介绍了开始Django:执行子选择和其他复杂查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Django中执行一个半复杂的查询。例如,我想要这样的东西:

  SELECT 
b。*,
(SELECT count (id)FROM comments c WHERE c.blog_id = b.id)AS number_of_comments
FROM blog b
WHERE 1

从我的PHP背景,代码点燃器和Zend框架有查询构建器。您可以使用框架中的方法构建SQL查询。这是Django吗?



在Django中构建和执行复杂查询的最佳方法是什么?是否有推荐的方式/最佳做法做这些类型的查询?



更新:



由于下面的mherren代码,我的工作变化很小。以下是代码的更新版本。



在我的views.py中,我有:

 def index(request):
blog_posts = Blog.objects.all()。annotate(Count('comment'))。order_by(' - pub_date')[:5]

返回render_to_response('blog / index.html',
{'blog_posts':blog_posts})

在我的模板文件(index.html)中,我有这个:

 欢迎... 
{%如果blog_posts%}
< ul>
{%for blog在blog_posts%}
< li>
< b>
< a href =/ blog / post / {{post.id}}> {{post.title}}< / a>
< / b> ({{post.pub_date}})< br />
{{post.content}}< br />
{{post.comment__count}}评论< br />
by:{{post.author}}< br />< br />
< / li>
{%endfor%}
< / ul>
{%else%}
< p>没有帖子可用。< / p>
{%endif%}

希望这也有助于别人。感谢所有的人!

解决方案

您可以使用 here



b
$ b

  class Post(models.Model):
title = models.CharField(max_length = 200)
body = models.TextField ()
#additional fields here ...

类注释(models.Model):
post = models.ForeignKeyField(Post)
#addditional fields here。

...

from django.db.models import Count
from project.application.models import Post,Comment

post_list = Post.objects.annotate(Count('comment_set'))
在post_list中的p:
print p.comment_set__count


I want to execute a semi-complex query in Django. For example I want something that is like this:

SELECT 
b.*,
(SELECT count(id) FROM comments c WHERE c.blog_id = b.id) AS number_of_comments
FROM blog b 
WHERE 1

From my PHP background, Code Igniter and Zend Framework has "query builders". Where you can built an SQL-query using the methods in the framework. Is this something like in Django?

What would be the best way to build and execute complex queries in Django? Is there a recommended way / best-practice to do these kinds of queries?

UPDATE:

I got it working with little changes thanks to mherren's code below. Here is the updated version of the code.

In my views.py I have this:

def index(request):
    blog_posts = Blog.objects.all().annotate(Count('comment')).order_by('-pub_date')[:5]

    return render_to_response('blog/index.html', 
    {'blog_posts': blog_posts})

In my template file (index.html) I have this:

Welcome...
{% if blog_posts %}
    <ul>
    {% for post in blog_posts %}
        <li>
            <b>
            <a href="/blog/post/{{ post.id }}">{{ post.title }}</a>
            </b> ({{ post.pub_date }})<br/>
            {{ post.content }}<br/>
            {{ post.comment__count }} comment(s)<br/>
            by: {{ post.author }}<br/><br/>
        </li>
    {% endfor %}
    </ul>
{% else %}
    <p>No posts are available.</p>
{% endif %}

Hope this also helps out the others. Thanks for everything guys!

解决方案

You can do this using aggregation described here.

Something the likes of:

class Post(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    #additional fields here...

class Comment(models.Model):
    post = models.ForeignKeyField(Post)
    #additional fields here...

...

from django.db.models import Count
from project.application.models import Post, Comment

post_list = Post.objects.annotate(Count('comment_set'))
for p in post_list:
    print p.comment_set__count

这篇关于开始Django:执行子选择和其他复杂查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆