chaleaoch
V2EX  ›  Django

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

  •  
  •   chaleaoch · May 11, 2016 · 3918 views
    This topic created in 3685 days ago, the information mentioned may be changed or developed.
    例如 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 replies    2016-05-11 23:56:44 +08:00
    ETiV
        1
    ETiV  
       May 11, 2016 via iPhone   ❤️ 1
    mysql 开 general log
    啥都能看见
    chaleaoch
        2
    chaleaoch  
    OP
       May 11, 2016
    自问自答:
    >>> from django.db import connection
    >>> connection.queries

    另,感谢楼上
    @ETiV
    lyhapple
        3
    lyhapple  
       May 11, 2016   ❤️ 1
    你需要 django debug tool 这个第三方库
    junzki
        4
    junzki  
       May 11, 2016   ❤️ 1
    roricon
        5
    roricon  
       May 11, 2016 via iPhone   ❤️ 1
    http://stackoverflow.com/a/2314982
    用的比较爽的解决方案
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1118 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 23:53 · PVG 07:53 · LAX 16:53 · JFK 19:53
    ♥ Do have faith in what you're doing.