Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get query for .save() in django?

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.

like image 470
Aamir Rind Avatar asked Nov 16 '11 07:11

Aamir Rind


1 Answers

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,

from django.db import connection, OperationalError
try:
    modelObj.save()
except OperationalError:
    print(connection.queries[-1])
like image 82
Gagandeep Singh Avatar answered Sep 23 '22 14:09

Gagandeep Singh