Django-配置mysql

注意:需要在项目中安装mysqlclient包

setting文件数据库相关修改:

python 复制代码
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite3',
        'USER': 'root',
        'PASSWORD': 'bai12345',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

什么是模型?:

ORM框架:

注册app需要在setting中添加:

python 复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'music',
    'news',
    'sport',
    'bookstore'
]
python 复制代码
from django.db import models


# Create your models here.
class Book(models.Model):
    title = models.CharField("书名", max_length=100)
    author = models.DecimalField('价格', max_digits=10, decimal_places=2)

执行后结果: 生成的表名为app名_小写类名

相关推荐
重庆小透明16 分钟前
【搞定面试之mysql】第三篇 mysql的锁
java·后端·mysql·面试·职场和发展
雕刻刀26 分钟前
ERROR: Failed to build ‘natten‘ when getting requirements to build wheel
开发语言·python
何双新27 分钟前
Odoo 技术演进全解析:从 Widget 到 Owl,从 Old API 到声明式 ORM
python
山川行1 小时前
关于《项目C语言》专栏的总结
c语言·开发语言·数据结构·vscode·python·算法·visual studio code
星辰徐哥1 小时前
C语言游戏开发:Pygame、SDL、OpenGL深度解析
c语言·python·pygame
xcLeigh1 小时前
Python入门:Python3基础练习题详解,从入门到熟练的 25 个实例(六)
开发语言·python·教程·python3·练习题
不懒不懒1 小时前
安装python3.9.7和pycharm-community-2022.3.2.exe以及linux
linux·ide·python·pycharm
lzhdim1 小时前
SQL 入门 7:SQL 聚合与分组:函数、GROUP BY 与 ROLLUP
java·服务器·数据库·sql·mysql
Jasonakeke1 小时前
我的编程来时路
java·c++·python
Yvonne爱编码1 小时前
Java 中的 hashCode () 与 equals () 核心原理、契约规范、重写实践与面试全解
java·开发语言·数据结构·python·hash