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

相关推荐
一勺菠萝丶3 分钟前
macOS 安装 Python 包报错:`externally-managed-environment` 怎么解决?
python
chushiyunen7 分钟前
r树索引、mysql对r树的支持
数据库·mysql
醒李32 分钟前
盲人出行辅助系统原型
人工智能·python·目标检测
罗超驿33 分钟前
16.深入理解数据库事务:从转账场景剖析ACID四大特性与回滚(Rollback)机制
数据库·mysql
PILIPALAPENG1 小时前
第4周 Day 3:多 Agent 协作——让 Agent 们"组队干活"
前端·人工智能·python
Omics Pro1 小时前
填补蛋白质组深度学习预处理教学空白
人工智能·python·深度学习·plotly·numpy·pandas·scikit-learn
@nengdoudou2 小时前
KingbaseES数据库MySQL模式使用 “GROUP BY“
数据库·mysql
万邦科技Lafite2 小时前
实战演练:利用京东API一键抓取商品详情
数据库·redis·python·缓存·开放api·淘宝开放平台
TheRouter2 小时前
OpenClaw 上下文瘦身:3 个实验
开发语言·python·ai
日光明媚2 小时前
TensorRT-LLM 中对 wan 加速流程与方法
人工智能·python·计算机视觉·stable diffusion·aigc