Frequently Asked Questions with django 2.2 and mysql

  • 2021-07-22 10:02:22
  • OfStack

Perhaps because the MySQLdb library used by Django does not support Python3, we used PyMySQL library instead, resulting in various pits, especially when the following two commands are executed:


python manage.py makemigrations
or
python manage.py inspectdb

Pit 1 (indicating that your mysqlclient version is too low)

If you are bored with the latest version of pip install mysqlclient installation, you will throw:


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

MD, LZ see this mistake too want to swear, no way to take online method, annotate Dafa!

Locate the Python36-32\ Lib\ site-packages\ django\ db\ backends\ mysql\ base.py file under the Python installation

Comment the following code in the file (you may need to close pycharm IDE first)


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

The second pit (str type does not have decode method)

Pair-to-pair, py3 defaults to str encoding, which is encoded into bytes type by encode method, and the latter has decode decoding method.
Prompt error source: Python36\ lib\ site-packages\ django\ db\ backends\ mysql\ operations. py ", line 149, in last_executed_query

Here online 1 search 1 heap of encode changed to decode method, I depend, whose brain hole is invincible
Source method content (django 2.2. 1 intact content of pip installation):


  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

Output query result through print solution, the content is as follows


SELECT @@SQL_AUTO_IS_NULL

 The data type is str

There are also annotation solutions on the Internet. LZ doesn't know what the sequelae of annotation if are and whether it has any influence, so it hasn't been adopted.

So I went to github of django to go through the latest/version history of this file and this method, and the latest master branch content is 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 SQL into str

Holy shit! ! ! This Nima official website 2.2. 1/2.2. 2 (current latest version) package is not harmful, remember to introduce this method above this file


from django.utils.encoding import force_str

Then execute the managa. py command, OK


Related articles: