Section thirty-ninth create and map a simple ORM model

Set setting.py

 1 DATABASES = {
 2     'default': {
 3         'ENGINE': 'django.db.backends.mysql',
 4         'NAME':'book_manager',
 5         'USER':'root',
 6         'PASSWORD':'',
 7         'HOST':'localhost',
 8         'PORT':3306,
 9 
10     }
11 }
1 INSTALLED_APPS = [
2     'django.contrib.admin',
3     'django.contrib.auth',
4     'django.contrib.contenttypes',
5     'django.contrib.sessions',
6     'django.contrib.messages',
7     'django.contrib.staticfiles',
8     'book',  # 将创建的APP注册
9 ]

 __Init__.py application with the same name, the code is written as follows

import pymysql
pymysql.install_as_MySQLdb()

Create models in models.py

from django.db Import Models 

class Book (models.Model): 
    the above mentioned id = models.AutoField (primary_key = True)
     # the above mentioned id can not write, django will create its own growth from a primary key 
    name = models.CharField (max_length = 100, null = False) 
    author = models.CharField (MAX_LENGTH = 100, null = False) 
    . price = models.FloatField (null = False, default = 0) 

'' ' 
in the directory where manger.py using python manager.py makemigrations generating migration scripts file 
using python manager.py migrate the generated migration script file mapping to the database, create a corresponding table 
'' '

The corresponding table in the database generated if not specified when creating a table name, it will create a table book_ django application name

 

Guess you like

Origin www.cnblogs.com/kogmaw/p/12458133.html