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

Aam*_*nan 6 python mysql django django-models

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

Gag*_*ngh 11

你可以试试

from django.db import connection
connection.queries
Run Code Online (Sandbox Code Playgroud)

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

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

  • 不要忘记将settings.DEBUG设置为True以查看查询 (4认同)