Django:将博客条目视图计数增加1。这是高效吗? [英] Django: Increment blog entry view count by one. Is this efficient?

查看:140
本文介绍了Django:将博客条目视图计数增加1。这是高效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的索引视图中有以下代码。

  latest_entry_list = Entry.objects.filter(is_published = True)。 order_by(' -  date_published')[:10] 
用于在latest_entry_list中输入:
entry.views = entry.views + 1
entry.save()

如果从初始查询返回了十(限制)行,保存问题10将更新的调用分离到数据库, Django聪明足以只发出一个更新调用?



有更有效的方法来实现这个结果吗?

您可以使用 F()对象。



以下是从django.db.models导入 F / code>



New in Django 1.1

调用更新还可以使用F()对象更新一个字段基于模型中的另一个字段的值。这对于根据当前值增加计数器特别有用。

  Entry.objects.filter(is_published = True).update views = F('views')+ 1)

切片查询集... edit:实际上您可以...



这可以在django ORM中完成。您需要两个SQL查询:


  1. 执行过滤器并收集主键列表


取得非切片的查询集是硬位。我想知道如何使用 in_bulk ,但返回字典,而不是查询集。通常会使用 Q objects 来执行复杂的OR类型查询,并且可以工作,但 pk__in 更简单。

  latest_entry_ids = Entry.objects.filter(is_published = True)\ 
.order_by(' - date_published')
.values_list('id',flat = True )[:10]
non_sliced_query_set = Entry.objects.filter(pk__in = latest_entry_ids)
n = non_sliced_query_set.update(views = F('views')+ 1)
print n或0, 'items updated'

由于django延迟执行查询的方式, ,无论多少项目都更新。


I have the following code in my index view.

latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10]
for entry in latest_entry_list:
    entry.views = entry.views + 1
    entry.save()

If there are ten (the limit) rows returned from the initial query, will the save issue 10 seperate updated calls to the database, or is Django "smart" enough to issue just one update call?

Is there a more efficient method to achieve this result?

解决方案

You can use F() objects for this.

Here is how you import F: from django.db.models import F

New in Django 1.1.
Calls to update can also use F() objects to update one field based on the value of another field in the model. This is especially useful for incrementing counters based upon their current value.

Entry.objects.filter(is_published=True).update(views=F('views')+1)

Although you can't do an update on a sliced query set... edit: actually you can...

This can be done completely in django ORM. You need two SQL queries:

  1. Do your filter and collect a list of primary keys
  2. Do an update on a non-sliced query set of items matching any of those primary keys.

Getting the non-sliced query set is the hard bit. I wondered about using in_bulk but that returns a dictionary, not a query set. One would usually use Q objects to do complex OR type queries and that will work, but pk__in does the job much more simply.

latest_entry_ids = Entry.objects.filter(is_published=True)\
                                      .order_by('-date_published')
                                      .values_list('id', flat=True)[:10]  
non_sliced_query_set = Entry.objects.filter(pk__in=latest_entry_ids)  
n = non_sliced_query_set.update(views=F('views')+1)  
print n or 0, 'items updated'

Due to the way that django executes queries lazily, this results in just 2 database hits, no matter how many items are updated.

这篇关于Django:将博客条目视图计数增加1。这是高效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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