Django's object-relational mapping database

 

Django ORM basic configuration

So far, our program when it comes to database-related operations, we will generally do so:

  • Create a database table structure and design field
  • MySQLdb used to connect to the database, and the code written in the data access layer
  • To call business logic layer data access layer to perform database operations

django using a new way, namely: object mapping relationship (Object Relational Mapping, referred to as the ORM), follow the principle of the django Code Frist, namely: to automatically generate a database table defined in accordance with the class code;

1, modify the configuration database project (the settings.py file in the main program directory)

The default connection to the local database file sqlite3:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Replace specified mysql database:

= DATABASES {
     ' default ' : {
         ' ENGINE ' : ' django.db.backends.mysql ' ,
         ' NAME ' : ' the mysite ' ,    # database name must be present in 
        ' the USER ' : ' XXXX ' ,
         ' PASSWORD ' : ' XXX ' ,
         ' the HOST ' : ' 192.168.xx.xx ' ,
        'PORT': '3306'
    }
}

2, creates the definition file database table structure (corresponding to the models.py app directory file)

Generate a simple database table:

from django.db import models

# Create your models here.

class UserInfo(models.Model):

    username = models.CharField(max_length=32)
    passwd = models.CharField(max_length=64)

The corresponding app name is added to the configuration file in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cmdb02',
]

3, generation of database

Execute the following command:

manage.py makemigrations Python 
Python manage.py the migrate          # create the tables

Note: Django default with MysqlDB modules connect to the database, but there has not python3.x this module, all modules need to connect to the database into pymsyql, modify init.py file in the project directory

import  pymysql
pymysql.install_as_MySQLdb()

Execute commands to generate a database table, the problems encountered:

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

  Solve : find base.py \ under python_path \ Lib \ site-packages \ django db \ backends \ mysql path, comment the following code:

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

Solve : find operations.py \ under python_path \ Lib \ site-packages \ django db \ backends \ mysql path, modify the following code:

 = Query Query. encode (errors = 'Replace') will be modified to encode decode

c. pymysql.err.InternalError: (1049, "Unknown database 'mysite'")

Solve : Database Create a database mysite

Guess you like

Origin www.cnblogs.com/lhly/p/11128056.html