#boards/models.py
from django.utils.text import Truncator

class Topic(models.Model):
# ...
def __str__(self):
return self.subject
class Post(models.Model):
# ...
def __str__(self):
truncated_message = Truncator(self.message)
return truncated_message.chars(30)

python manage.py shell
from boards.models import Board
# First get a board instance from the database
board = Board.objects.get(name='Django')
board.topics.all()
board.topics.count()
#现在统计一个版块下面的回复数量有点麻烦,因为回复并没有和 Board 直接关联
from boards.models import Post
Post.objects.all()
Post.objects.count()
这里一共11个回复,但是它并不全部属于 "Django" 这个版块的。我们可以这样来过滤
from boards.models import Board, Post
board = Board.objects.get(name='Django')
Post.objects.filter(topic__board=board)
Post.objects.filter(topic__board=board).count()
最后一个任务是标识版块下面的最后一条回复
Post.objects.filter(topic__board=board).order_by('-created_at')
Post.objects.filter(topic__board=board).order_by('-created_at').first() #boards/models.py
class Board(models.Model):
def get_posts_count(self):
return Post.objects.filter(topic__board=self).count()
def get_last_post(self):
return Post.objects.filter(topic__board=self).order_by('-created_at').first() <--templates/home.html-->
<!--以下代码应该在<small></small>中但是未通过-->
<a href="{% url 'topic_posts' board.pk post.topic.pk %}">
   By {{ post.created_by.username }} at {{ post.created_at }}
</a> {% extends 'base.html' %}
{% block breadcrumb %}
<li class="breadcrumb-item active">Boards</li>
{% endblock %} {% block content %}
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Board</th>
<th>Posts</th>
<th>Topics</th>
<th>Last Post</th>
</tr>
</thead>
<tbody>
{% for board in boards %}
<tr>
<td>
<a href="{% url 'board_topics' board.pk %}">{{ board.name }}</a>
<small class="text-muted d-block">{{ board.description }}</small>
</td>
<td class="align-middle">{{ board.get_posts_count }}</td> <td class="align-middle">{{ board.topics.count }}</td>
<td class="align-middle">
{% with post=board.get_last_post %}
<small>
</small>
{% endwith %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
<!--看起来好像有问题,如果没有回复的时候程序会崩溃-->
<!--templates/home.html-->
{% with post=board.get_last_post %}
{% if post %}
<small>
<a href="{% url 'topic_posts' board.pk post.topic.pk %}">
By {{ post.created_by.username }} at {{ post.created_at }}
</a>
</small>
{% else %}
<small class="text-muted">
<em>No posts yet.</em>
</small>
{% endif %}
{% endwith %}
python manage.py shell
from django.db.models import Count
from boards.models import Board
board = Board.objects.get(name='Django')
topics = board.topics.order_by('-last_updated').annotate(replies=Count('posts'))
for topic in topics:
print(topic.replies) #我们来做一个小小的修复,因为回复里面不应该包括发起者的帖子
topics = board.topics.order_by('-last_updated').annotate(replies=Count('posts') - 1)
for topic in topics:
print(topic.replies)
#boards/views.py 更新
from django.db.models import Count
def board_topics(request, pk):
board = get_object_or_404(Board, pk=pk)
topics = board.topics.order_by('-last_updated').annotate(replies=Count('posts') - 1)
return render(request, 'topics.html', {'board': board, 'topics': topics}) <!--templates/topics.html-->
{% for topic in topics %}
<tr>
<td><a href="{% url 'topic_posts' board.pk topic.pk %}">{{ topic.subject }}</a></td>
<td>{{ topic.starter.username }}</td>
<td>{{ topic.replies }}</td>
<td>0</td>
<td>{{ topic.last_updated }}</td>
</tr>
{% endfor %}

Django入门与实践-第20章:QuerySets(查询结果集)(完结)的更多相关文章

  1. Django入门与实践-第26章:个性化工具(完结)

    http://127.0.0.1:8000/boards/1/topics/62/reply/ 我觉得只添加内置的个性化(humanize)包就会很不错. 它包含一组为数据添加“人性化(human t ...

  2. Django入门与实践-第23章:分页实现(完结)

    http://127.0.0.1:8000/boards/1/ #从现在起,我们将在 board_topics 这个视图中来操作. python manage.py shell from django ...

  3. Django入门与实践-第12章:复用模板(完结)

    http://127.0.0.1:8000/http://127.0.0.1:8000/boards/1/http://127.0.0.1:8000/boards/2/http://127.0.0.1 ...

  4. Django入门与实践-第11章:URL 分发(完结)

    http://127.0.0.1:8000http://127.0.0.1:8000/boards/1/http://127.0.0.1:8000/boards/2/http://127.0.0.1: ...

  5. Django入门与实践-第25章:Markdown 支持(完结)

    http://127.0.0.1:8000/boards/1/topics/102/reply/ 让我们在文本区域添加 Markdown 支持来改善用户体验. 你会看到要实现这个功能非常简单. 首先, ...

  6. Django入门与实践-第19章:主题回复(完结)

    http://127.0.0.1:8000/boards/1/topics/1/reply/ http://127.0.0.1:8000/boards/1/topics/1/ #myproject/u ...

  7. Django入门与实践-第16章:用户登录(完结)

    # myproject/settings.py LOGIN_REDIRECT_URL = 'home' EMAIL_BACKEND = 'django.core.mail.backends.conso ...

  8. Django入门与实践-第14章:用户注册(完结)

    http://127.0.0.1:8000/signup/ django-admin startapp accounts INSTALLED_APPS = [ 'accounts', ] # mypr ...

  9. Django入门与实践-第13章:表单处理(完结)

    http://127.0.0.1:8000/boards/1/ http://127.0.0.1:8000/boards/2/ http://127.0.0.1:8000/boards/3/ http ...

随机推荐

  1. MySQL优化order by导致的 using filesort

    using filesort 一般出现在 使用了 order by 语句当中. using filesort不一定引起mysql的性能问题.但是如果查询次数非常多,那么每次在mysql中进行排序,还是 ...

  2. Springmvc Exception

    对于异常处理,大多使用一个共同的异常类统一处理从dao,service,controller层抛出的异常,将页面跳转到共同的error页面. public class CommonException ...

  3. vue基础——事件处理

    监听事件 可以用 v-on 指令监听dom事件,并在触发时运行一些JavaScript代码. 示例: <div id="example-1"> HTML <but ...

  4. MySQL修改编码设置及乱码问题

    源地址:http://blog.csdn.net/millia/article/details/5806774   昨天尝试把自己用php编写的第一个糙站发布到网上..结果出现了因为编码不统一而导致乱 ...

  5. 复制CentOS虚拟机网络配置

    复制出来的CentOS虚拟机,网络需要重新配置.   卸载原来的VMware网卡,重新启用一块新的网卡,网卡网段要匹配.   ifconfig -a 查看当前启用网卡的mac地址 编辑/etc/ude ...

  6. C++的空指针、野指针和指针赋值NULL.md

    1.空指针和野指针 http://blog.csdn.net/fu_zk/article/details/21030607 空指针常量 一个表示0值的整数常量,叫做空指针常量.例如:0.0L.1-1( ...

  7. 使用github的流程

    使用github的流程 在实际项目开发中,按照如下步骤使用git进行代码管理 1.项目经理在开发之初,创建好仓库,上传项目的框架.组员分支 2.组员克隆项目框架,同步分支,按分工开发,在分支提交代码 ...

  8. HTTP request failed! HTTP/1.1 411 Length Required

    $opts = array( 'http'=>array( 'method'=>"POST", 'header' => 'Content-Length: 0' / ...

  9. Spring @Configuration

    下面是一个典型的spring配置文件(application-config.xml): <beans> <bean id="orderService" class ...

  10. Required String parameter ' ' is not present

    Required String parameter ' ' is not present 报错原因: url中的参数错误. 解决方法: 1.修正url中的参数的值. 2.在Controller层中的@ ...