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

时间:2014-07-25 16:48:21   收藏:0   阅读:265
  1. 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()

     

  2. 简写了代码:
    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直接执行就爽得不行,偏要搞一层又一层,还不如直接上!

django - from django.db.models import F - class F,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!