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名_小写类名

相关推荐
极梦网络无忧5 小时前
OpenClaw 基础使用说明(中文版)
python
别抢我的锅包肉6 小时前
【MySQL】第四节 - 多表查询、多表关系全解析
数据库·mysql·datagrip
codeJinger6 小时前
【Python】操作Excel文件
python·excel
XLYcmy6 小时前
一个针对医疗RAG系统的数据窃取攻击工具
python·网络安全·ai·llm·agent·rag·ai安全
zzh0817 小时前
MySQL高可用集群笔记
数据库·笔记·mysql
Islucas7 小时前
Claude code入门保姆级教程
python·bash·claude
萝卜白菜。7 小时前
TongWeb7.0相同的类指明加载顺序
开发语言·python·pycharm
赵钰老师7 小时前
【ADCIRC】基于“python+”潮汐、风驱动循环、风暴潮等海洋水动力模拟实践技术应用
python·信息可视化·数据分析
Shely20177 小时前
MySQL数据表管理
数据库·mysql