开始Django:执行子选择和其他复杂查询 [英] Beginning Django: performing sub-selects and other complex queries
问题描述
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屋!