VS2019 development Django (c) ------ connection MySQL

Come home, wash finished, late at night. About this series Django blog, my heart's content before the idea is to undertake a program of small micro-channel, make a server-side management center, a new menu, adjust the price ah! And the like, have an idea, to complete a series of learning, or else aimlessly, not knowing insist on significance.

Previous, we have completed the first Django app hello world, this is our subject matter MySql database connection, because the work which is used MySql database, and I also have this environment, then consider using direct the MySql database. MySql installation on services, not described here, and we go straight.

1) modify the configuration language code zone, setting.py locate files, modifying inside language code: ZH-Hans , when the modified region is: Asia / of Shanghai

# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True

2) modify the configuration database, official documents mention a total of the following four database engine, the rest of the configuration is the connection information for your database, after the configuration is complete, be sure to create a new database in MySql in DjangoLazyOrders

'django.db.backends.postgresql'
'django.db.backends.mysql'
'django.db.backends.sqlite3'
'django.db.backends.oracle'

# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DjangoLazyOrders',
        'USER': 'test',
        'PASSWORD': '123456',
        'HOST': '127.0.0.1',
        'PORT': '10080',
    }
}

3) installation pymysql , similar to that used in MySql.Data.dll .Net, so understanding on the line. First cmd switch to project a virtual environment Scripts files folder (D: \ projects \ local \ DjangoLazyOrders \ env \ Scripts ), then enter the following command, enter, wait for the installation to complete, after the installation is complete, we can see that we the installed version is 0.9.3, which is the latest version. RE: be sure to switch to the virtual environment to install

pip install pymysql

 4) adding Models, official documents copied from the following entities, wherein __str__ function, similar to the C # override ToString () method

from django.db import models

# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

5) data migration, as shown below, Django choose to migrate directly anomaly ModuleNotFoundError: No module named 'MySQLdb' can not find MySQLdb this module, how to solve this problem?

 Open __init __ py (hello) file, paste the following code into it, migrate again, a problem has been solved, but immediately throw a new exception: django.core.exceptions.ImproperlyConfigured: the mysqlclient 1.3.13 or newer IS required;. you have 0.9.3 configuration requires version 1.3.13 or later, and you're using version 0.9.3, how to solve this problem? See the error message mentions a file D: \ projects \ local \ DjangoLazyOrders \ env \ lib \ Site-Packages Standard Package \ Django \ db \ backends \ MySQL \ base.py , open the file, search directly 1.3.13 locate the code block , annotated version check code.

import pymysql
pymysql.install_as_MySQLdb()

 Then try again to migrate, WTF, again an exception: AttributeError: 'str' Object attribute has NO 'decode' , no way, only to find once again prompt D: \ projects \ local \ DjangoLazyOrders \ env \ lib \ site-packages \ django \ db \ backends \ mysql \ operations.py files, direct search errors = 'replace', is positioned to block, and then the two lines in the figure commented

 Try again to migrate, and finally no abnormalities, and then choose to migrate, finally, finally, a success.

正在执行 manage.py makemigrations
No changes detected
正在执行 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, hello, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying hello.0001_initial... OK
  Applying sessions.0001_initial... OK
>>> 

Then we went to view the database, the database migration is successful, we add an entity, corresponding table hello_choice, hello_question

Sleepy .... To be continued ....

Guess you like

Origin www.cnblogs.com/dwBurning/p/Django3.html