如何在django中获取.save()的查询?

时间:2022-05-15 11:41:28

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.

我正在更新一个django模型对象。当我调用obj.save()时为每个属性设置值后,它给了我一个OperationalError :( 2006,'MySQL服务器已经消失')。我迫不及待地想知道导致以下错误的原因。我怎样才能得到查询?由于上述错误导致保存方法失败,因此不会记录查询。有什么建议么??提前致谢。

1 个解决方案

#1


10  

You can try

你可以试试

from django.db import connection
connection.queries

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

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

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

#1


10  

You can try

你可以试试

from django.db import connection
connection.queries

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

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

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