请教关于 django 的 orm 语句同时修改多条数据以及 flush 操作 - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
kayseen

请教关于 django 的 orm 语句同时修改多条数据以及 flush 操作

  •  
  •   kayseen Apr 16, 2020 3546 views
    This topic created in 2240 days ago, the information mentioned may be changed or developed.

    tips: django 使用的 mysql, 以往用的是 flask 的 sqlalchemy, 比如同时修改了两个不同表的两条数据 user1 的 name=1,class2 的 name=class2,(两条数据没有关联关系), 我可以 model.add(user1),model.add(class2),最后 model.commit()一次性提交,

    但是在 django 的 orm 中, 如果有上述的场景, 一次性提交两条修改的数据(开事物?),该怎么实现呢?

    找到了 django 的批量创建 bulk_update(), 但是此方法只支持批量更新同张表的多条数据,并且需要指定 fields

    问题: 1.批量修改多张表的多条数据怎么一次性提交保存? 2.django 有没有在提交数据之前的 flush 操作(在创建新数据的时候,flush 操作可以获取新数据的 id,但是此时数据并没有保存到 mysql 中)

    5 replies    2020-04-16 10:05:29 +08:00
    chen774553620
        1
    chen774553620  
       Apr 16, 2020
    from django.db import transaction
    with transaction.atomic:
    kayseen
        2
    kayseen  
    OP
       Apr 16, 2020
    @chen774553620
    了解, 真棒~
    kayseen
        4
    kayseen  
    OP
       Apr 16, 2020
    @Vegetable
    这个东西看过啦哈哈, 请问您知道关于 django 的 flush 操作吗?
    EricCartmann
        5
    EricCartmann  
       Apr 16, 2020 via iPhone
    一楼已经终结这个问题。transaction 模块完美解决,加上 with transaction.atomic()还可以提供事务的特性,引入一个 savepoint 还可以回滚。
    About     Help     Advertise     Blog     API     FAQ     Solana     1550 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 16:46 PVG 00:46 LAX 09:46 JFK 12:46
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86