V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chaleaoch
V2EX  ›  Django

我想知道 django 的 ORM 在 create 或 save 的时候执行的 sql 语句,有没有办法查询到?

  •  
  •   chaleaoch · 2016-05-11 18:52:38 +08:00 · 3290 次点击
    这是一个创建于 2908 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如 queryset 我们可以这样:

    >>> a =Post.objects.all()
    >>> a
    [<Post: >, <Post: hello_title>]
    >>> print a.query
    SELECT `posts_post`.`id`, `posts_post`.`title`, `posts_post`.`content`, `posts_post`.`publish`, `posts_post`.`updated` FROM `posts_post` ORDER BY `posts_post`.`updated` DESC, `posts_post`.`publish` DESC
    >>> type(a)
    <class 'django.db.models.query.QuerySet'>

    但是 create 或 save 的时候怎么办?
    求菊苣不吝告之。。。
    5 条回复    2016-05-11 23:56:44 +08:00
    ETiV
        1
    ETiV  
       2016-05-11 19:00:34 +08:00 via iPhone   ❤️ 1
    mysql 开 general log
    啥都能看见
    chaleaoch
        2
    chaleaoch  
    OP
       2016-05-11 19:03:18 +08:00
    自问自答:
    >>> from django.db import connection
    >>> connection.queries

    另,感谢楼上
    @ETiV
    lyhapple
        3
    lyhapple  
       2016-05-11 22:38:59 +08:00   ❤️ 1
    你需要 django debug tool 这个第三方库
    junzki
        4
    junzki  
       2016-05-11 23:35:42 +08:00   ❤️ 1
    roricon
        5
    roricon  
       2016-05-11 23:56:44 +08:00 via iPhone   ❤️ 1
    http://stackoverflow.com/a/2314982
    用的比较爽的解决方案
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1821 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:19 · PVG 00:19 · LAX 09:19 · JFK 12:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.