您的位置:首页 > 编程语言 > Go语言

django - from django.db.models import F - class F

2014-07-25 14:15 351 查看
F() 的执行不经过 python解释器,不经过本机内存,是生成 SQL语句的执行。

# Tintin filed a news story!
reporter = Reporters.objects.get(name='Tintin')
reporter.stories_filed += 1
reporter.save()

# 等于

from django.db.models import F
reporter = Reporters.objects.get(name='Tintin')
reporter.stories_filed = F('stories_filed') + 1
reporter.save()


简写了代码:

Reporter.objects.all().update(stories_filed=F('stories_filed') + 1)


这就是F()的使用了。


使用F()的原因就是:

F() therefore can offer performance advantages by:

getting the database, rather than Python, to do work
reducing the number of queries some operations require

提供了性能优势:
getting方法,也就是查询操作,更快
减少了某些操作的大量查询

你看这事情弄得多蛋疼?
原本SQL直接执行就爽得不行,偏要搞一层又一层,还不如直接上!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: