Django2.2报错 AttributeError: 'str' object has no attribute 'decode'
Prepare to connect Django to MySQL and python manage.py makemigrations
report AttributeError: 'str' object has no attribute 'decode'
an error after entering the command on the command line: After this error occurs, you can find the file location according to the error prompt, open the operations.py file, and find the following code:
def last_executed_query(self, cursor, sql, params):
# With MySQLdb, cursor objects have an (undocumented) "_executed"
# attribute where the exact query sent to the database is saved.
# See MySQLdb/cursors.py in the source distribution.
query = getattr(cursor, '_executed', None)
if query is not None:
query = query.decode(errors='replace')
return query
The error message indicates, if an error description statement is executed, a query type str, and decode()
is used to convert the bytes into a string type, (here about Python code points) , since the query does not need to decode, so the if statement directly comment Just drop it
def last_executed_query(self, cursor, sql, params):
# With MySQLdb, cursor objects have an (undocumented) "_executed"
# attribute where the exact query sent to the database is saved.
# See MySQLdb/cursors.py in the source distribution.
query = getattr(cursor, '_executed', None)
# if query is not None:
# query = query.decode(errors='replace')
return query