In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how to view the SQL statements executed by ORM in Django. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The encapsulation of database operations in Django ORM is quite perfect, and most of the daily database operations can be realized through ORM.
But django hides the query process in the background, which may be a little obscure at development time, and it can be too expensive to use it improperly.
So how do you see when django executes what sql statements? The answer is to use Logging.
First, add the LOGGING option to settings.py, and adjust the logging level to DEBUG:
LOGGING = {'version': 1,' disable_existing_loggers': False, 'formatters': {' simple': {'format':' [% (asctime) s]% (message) s'},}, 'handlers': {' console': {'level':' DEBUG', 'class':' logging.StreamHandler' 'formatter':' simple'},}, 'loggers': {' django': {'handlers': [' console'], 'level':' DEBUG',},},}
Then start runserver, browse the pages that need to access the database, and you can see the relevant logs in shell, as follows:
[2018-04-21 21 21 blog_ 09VR 14676] (0.002) SELECT `blog_ roomle`.`id`, `blog_ roomle`.`title`, `blog_ roomle`.`cover`, `blog_ roomle`.`pub _ date`, `blog_ roomle`.`category _ id`, `blog_ roomle`.`views`, `blog_ roomy`.`id` `blog_ roomy`.`name` FROM `INNER JOIN `blog_ roomy`ON (`blog_ roomle`.`category _ id` = `blog_ roomy`.`id`) WHERE `blog_ roomle`.`pub _ date` < '2018-04-21 13V 09VA 14.601856' ORDER BY `blog_ roomle`.`pub _ date` DESC LIMIT 10 Args= ('2018-04-21 13 AS 09blog_article_ 14.601856) [2018-04-21 2121 09VR 14678] (0.000) SELECT (`blog_article_ topics`.`article _ id`) AS `_ prefetch_related_val_article_ id`, `blog_ topic`.`id`, `blog_ topic`.`name`, `blog_ topic`.number``blog_ topic`INNER JOIN `blog_article_ topics` ON (`blog_article_ topic`.`id` = `blog_article_ topics`.topic _ id`) WHERE `blog_article_ topics`.article _ id` (3, 4, 5, 6, 7, 8) 9, 10, 11, 12) ORDER BY `blog_ topic`.`number`ASC Args= (3, 4, 5, 6, 7, 8, 9, 10, 11, 12) [2018-04-21 21 21 HTTP/1.1 0915 14708] "GET / HTTP/1.1" 200 22325
The log printed above is part of the SQL statement executed when my blog got the first ten articles on the home page of my blog. The corresponding QuerySet is
Article.objects.filter (pub_date__lt=timezone.now ()) [: 10]\ .defer ('author',' category__number')\ .select_related ('category')\ .prefetch_related (' topics')
Through Logging, you can not only view the SQL statement, but also know when django executed the SQL. In some cases, we can judge in this way whether the SQL statement has been repeatedly executed in the background, which is convenient to guide database access optimization.
Django uses Python's built-in logging module to perform system logging
The above is how to view the SQL statements executed by ORM in Django. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.