django set mysql database notes

1, guest / settings.py add in

import pymysql
pymysql.install_as_MySQLdb()

 Installed pymysql

2, DATABASES set parameters guest / settings.py below,

= {DATABASES 
    'default': { 
        'ENGINE': 'django.db.backends.mysql', 
        'the HOST': 'Database IP', 
        'PORT': 'Database port', 
        'NAME': 'database name', 
        ' uSER ':' msql user name ', 
        ' pASSWORD ':' MySQL password ', 
        ' the OPTIONS ': { 
            ' init_command ': "the SET = the sql_mode' the STRICT_TRANS_TABLES '", 
        }, 

    } 
}

3, the emergence of the list of issues

ImproperlyConfigured: the mysqlclient 1.3.13 or newer IS required
1, the background issue:
: already installed pymysql, but the command line python3 manage.py migrate data migration Times the following error
when (of course, you may be performing other operations encountered a similar error)

......

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", Line 127, in import_module
return _bootstrap._gcd_import (name [Level:], Package, Level)
File " /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/mysql/base.py ", Line 36, in <Module1>
The raise ImproperlyConfigured ( 'the mysqlclient 1.3. or newer iS required 13; you have have% S 'Database% .__ version__).
django.core.exceptions.ImproperlyConfigured: the mysqlclient 1.3.13 or newer iS required; you have have 0.9.3.

When prompted last two lines, mysqlclient version is not enough new, but I have installed the latest version of it.

2, the cause of the problem
PyMySQL although local drive has been installed, but still use the default connection MySQLdb driven Django MySQL, but does not support MySQLdb Python3, so the need to manually configure the project.

Add the following code to the __init__.py file in the root directory of the project:

pymysql Import
pymysql.install_as_MySQLdb ()
1
2
executing the command again, or will be error, it does not matter, the third line being given a closer look, told you are wrong base.py in the 36th row of the report, based on your path Tips open base.py, preceded by the line 35, 36 # commented out just fine, like this:

Version = Database.version_info 34 is
35 #if Version <(. 1,. 3, 13 is):
36 # The raise ImproperlyConfigured ( 'the mysqlclient 1.3.13 or newer IS required;% S have have you.'% Database .__ version__)

now execute the command again, the above error is gone, but has a new error, see how to solve it below.

AttributeError: 'str' object has no attribute 'decode'
to solve the above problems after he encountered the following error:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", Line 146, in last_executed_query
Query = query.decode (errors = 'the replace')
AttributeError: 'str' Object attribute has nO 'decode'

prompt property error: "str" object does not attribute "decode".

Cause of the problem is that in Python3 in:

str by encode () converted to bytes
bytes by decode () converts into str
that is to say: str only encode () method, bytes only decode () method!
This estimate is a bug in django.

Solution: When prompted to open the file operations.py being given

Found line 146, to encode into the decode, similar to the following:
140 DEF last_executed_query (Self, Cursor, SQL, the params):
141 is # With MySQLdb, Cursor Objects have have AN (sometimes called undocumented) "_executed"
142 # Exact The attribute WHERE Sent to IS The Database Query saved.
143 # See MySQLdb / cursors.py The Source Distribution in.
144 Query = getattr (Cursor, '_executed', None)
145 None Not IF IS Query:
146 Query = query.encode (errors = 'replace') # here to decode changed to encode
147 return Query

In this case, execute the command again is not an error, you're done!

  

Guess you like

Origin www.cnblogs.com/lza945/p/11448318.html