Home  >  Article  >  Backend Development  >  Tutorial introduction to using mysql with the django framework (code example)

Tutorial introduction to using mysql with the django framework (code example)

不言
不言forward
2019-03-06 13:57:482270browse

This article brings you a tutorial introduction (code example) about using mysql in the Django framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Let’s explain the use of the orm framework based on the creation of the django project

Note: First create a database in mysql manually or through commands. I first create a database named orm.

1: Configure the mysql database link string and time zone configuration in the settings.py file in the project folder

# 注册app
INSTALLED_APPS = [
    'teacher',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
# 配置数据库链接字符串
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'crm',
        'USER': '数据库用户名',
        'PASSWORD': '******',
        'HOST': '192.168.31.175',
        'PORT': '3306'
    }
}
# 设置时间时区
TIME_ZONE = 'Asia/Shanghai'

2: Add:

import pymysql
pymysql.install_as_MySQLdb()

to the __init__.py file in the project folder 3: Create entities in the models.py file in the app directory

from django.db import models
# Create your models here.
class Student(models.Model):
    name = models.CharField(max_length=20, verbose_name='姓名')
    age = models.SmallIntegerField(default=18, verbose_name='年龄')
    sex = models.SmallIntegerField(default=1, verbose_name='性别')
    qq = models.CharField(max_length=20, default='', verbose_name='qq')
    phone = models.CharField(max_length=20, default='', verbose_name='手机号')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __repr__(self):
        return "student<id=%s,name=%s,age=%s,sex=%s,qq=%s,phone=%s,create_time=%s>" % (
        self.id, self.name, self.age, self.sex, self.qq, self.phone, self.create_time)

4: Link the development environment through pycharm or xshell The Linux system enters the root directory of the Django project and executes the generated migration file

python manage.py makemigrations teacher

##4-1: View the SQL statement instructions generated by the migration file:

python manage.py sqlmigrate teacher 0001_initial.py


Then get the generated file through pycharm as follows:

5: Execute the migration file to generate the database table

python manage.py migrate

View the generated data table through navicat software:

The above is the detailed content of Tutorial introduction to using mysql with the django framework (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete