一、事务操作

  事务指的是只要全部完成,我提交数据才能算成功,只要有一个不成功,我就不提交。原理:mysql 每执行创建一条数据 就commit 一次。然后事务就是 当做一个整体  ,最后 commit 数据

django创建事务:

from django.db import transaction

with transaction.atomic():
    # 里面的代码 都遵循事务操作
    tags = from.cleaned_data.pop('tags')
    ....

二、数据库操作

2.1、写原生sql

from  app01 import models
# mysql:date_format(create_time,'%Y-%m')
date_list = models.Article.objects.raw(
    "select nid,count(nid) as num,strftime('%Y-%m',create_time) as ctime from app01_article group by strftime('%Y-%m',create_time)")

2.2、占位符

说明:在%s之前加一个% 表示 不是占位符,就是  %s

>>> a = "%s %%s"%(1)
>>> a
'1 %s'

2.3、日期筛选

article = models.Article.objects.filter(blog=blog).extra(
    where=['strftime('%Y-%m',create_time)=%s'],params=[val,]).all()

# select * from article where strftime('%Y-%m',create_time)=2017-02

 

相关文章:

  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-14
  • 2021-08-13
  • 2021-12-30
  • 2021-09-30
  • 2021-10-14
  • 2021-12-19
相关资源
相似解决方案