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直接执行就爽得不行,偏要搞一层又一层,还不如直接上!

相关文章:

  • 2022-12-23
  • 2022-01-12
  • 2021-07-04
  • 2021-11-18
  • 2021-10-19
  • 2021-11-12
  • 2021-11-30
猜你喜欢
  • 2021-08-07
  • 2021-05-31
  • 2021-07-17
  • 2022-02-25
  • 2022-12-23
  • 2022-01-15
  • 2022-02-21
相关资源
相似解决方案