如何在Django中查询.save()? [英] How can i get query for .save() in django?

查看:167
本文介绍了如何在Django中查询.save()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新Django模型对象.为每个属性设置值后,当我调用obj.save()时,它给了我OperationalError: (2006, 'MySQL server has gone away').我很想知道是什么导致以下错误.我如何获得查询?由于上述错误导致保存方法失败时,它不会记录查询. 有什么建议??预先感谢.

I am updating a django model object. After setting value for each attribute when i call obj.save() it gaves me OperationalError: (2006, 'MySQL server has gone away'). I am desperate to know what is causing the following error. How can i get the the query?? As when save method fail because of above error it does not log query. Any suggestions?? Thanks in advance.

推荐答案

您可以尝试

from django.db import connection
connection.queries

它将为您提供通过Django执行的所有查询的列表(包括.save()).要获取查询,您可以做到

it will give you list of all the queries that executed through Django (including .save()). To get your query you can do,

from django.db import connection, OperationalError
try:
    modelObj.save()
except OperationalError:
    print(connection.queries[-1])

这篇关于如何在Django中查询.save()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆