Several django 2.2 and pit use mysql

Django is probably due to the use of MySQLdb library does not support Python3, we adopted PyMySQL library instead, resulting in a variety of pits, in particular the implementation of the following two commands is the time:

python manage.py makemigrations
or
python manage.py inspectdb

The first pit (mysqlclient prompt your version is too low)

Are you bored running the latest version pip install mysqlclient installed, are thrown:

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None

MD, LZ see this error too want to call someone, and no way to take the online method, comments Dafa!

Find Python36-32 \ Lib \ site-packages \ django \ db \ backends \ mysql \ base.py files in Python installed Road King

The following code annotation file (may be required to close pycharm IDE)

if version < (1, 3, 3):
    raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

The second hole (str type not decode method)

Pair of pair of pair of, Py3 is unicode str default encoding type encoded into bytes encode method by which only the decoding method decode.
Tip Error Source: Python36 \ lib \ site-packages \ django \ db \ backends \ mysql \ operations.py ", line 149, in last_executed_query

  • Here's a bunch of online search to encode into decode methods I rely on, who's brain-hole invincible

SUMMARY source (PIP content installed intact django 2.2.1):

    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

Gong query print output results, content

SELECT @@SQL_AUTO_IS_NULL

数据类型为str
  • Here there are the online comments Dafa, LZ do not know if the comment is valid and sequelae have no effect, and it will not be adopted.

So I went to look through the latest django's github / historical version of a file of this method, the result of the latest master branch reads as follows:

    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.
        # MySQLdb returns string, PyMySQL bytes.
        return force_str(getattr(cursor, '_executed', None), errors='replace')

Look at the function name, it should be forced to convert to SQL str up
I rely on! ! ! This Nima official website 2.2.1 package is not harm it, remember this above method introduced in this document

from django.utils.encoding import force_str

Managa.py then execute the command, it

Guess you like

Origin blog.51cto.com/smilepad/2412322