Django model layer model (models.py) of creation

  Django database operation is a very important element, these two days to learn the simple operation of the database, to be a summary here.

  1.ORM Profile

  Simply put, ORM is the object - relational - mapping. It enables the decoupling of data models and databases, namely the design of the data model does not need to depend on a specific database, through a simple configuration can easily replace the database.

  2. Create Preparing model

  In the settings in the configuration databases

  DATABASES = {

  'default': {

  'ENGINE': 'django.db.backends.mysql',

  'NAME': 'myorm_test', the name of the database you want to connect #

  'USER': 'xxx', connect to the database user name #

  'PASSWORD': 'xxx', # corresponding user name password

  'HOST': '127.0.0.1', # connection to the host, where the machine is

  'PORT': 3306 # port, the default is 3306

  }

  }

  Introducing package pymysql

  Introduced in setting the packet pymysql

  __Init__.py file written under the name of the project file:

  import pymysql

  pymysql.install_as_MySQLdb()

  3. Create a model

  The new class models.py

  from django.db import models

  # Create your models here.

  # Book list

  class Book(models.Model):

  id = models.AutoField(primary_key=True)

  name = models.CharField(max_length=50)

  price = models.DecimalField(max_digits=7,decimal_places=2)

  # Max_digits: total number of digits (excluding the decimal point and symbol), decimal_places: number of decimal places

  publishs = models.ManyToManyField(to="publish")

  authors = models.ManyToManyField(to="author")

  # Press List

  class Publish(models.Model):

  id = models.AutoField(primary_key=True)

  name = models.CharField(max_length=50)

  addr = models.CharField(max_length=100)

  # List of authors hospital Wuxi gynecologist of http://www.ytsgfk120.com/

  class Author(models.Model):

  id = models.AutoField(primary_key=True)

  name = models.CharField(max_length=30)

  country = models.CharField(max_length=20)

  4. A data migration command at the terminal

  python manage.py makemigrations

  python manage.py migrate

  Problems encountered !!!

  Python manage.py makemigrations command execution, error:

  File "E:\PycharmProjects\MyORM_Test\venv\lib\site-packages\django\db\backends\mysql\base.py", line 36, in

  raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

  django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

  solve:

  In the "E: \ PycharmProjects \ MyORM_Test \ venv \ lib \ site-packages \ django \ db \ backends \ mysql \ base.py", line 36 position commented following code:

  # if version < (1, 3, 13):

  # raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

  Again to continue the implementation of python manage.py makemigrations error:

  File "E:\PycharmProjects\MyORM_Test\venv\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query

  query = query.decode(errors='replace')

  AttributeError: 'str' object has no attribute 'decode'

  solve:

  In the "E: \ PycharmProjects \ MyORM_Test \ venv \ lib \ site-packages \ django \ db \ backends \ mysql \ operations.py", line 146 and modify the following positions:

  query = getattr(cursor, '_executed', None)

  if query is not None:

  # Query = query.decode (errors = 'replace') # original code

  query = query.encode (errors = 'replace') # and modified

  return query

  problem solved.

Guess you like

Origin www.cnblogs.com/djw12333/p/11429771.html