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

相关推荐
Emotional。3 小时前
2025 年度技术总结与规划:AI 时代的开发者成长之路
人工智能·python·ai·langchain
witAI7 小时前
**AI仿真人剧制作软件2025推荐,解锁沉浸式数字内容创作
人工智能·python
Codefengfeng8 小时前
Python Base环境中加包的方法
开发语言·python
清水白石0088 小时前
《Python 编程全景解析:从核心精要到测试替身(Test Doubles)五大武器的实战淬炼》
开发语言·python
xuzhiqiang07249 小时前
MySQL——数据库的操作
数据库·mysql·oracle
如若1239 小时前
AutoDL云服务器 NVIDIA 570驱动 EGL渲染修复全记录
运维·服务器·python
甲枫叶9 小时前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
清水白石0089 小时前
《Python 编程全景解析:从核心精要到 Hypothesis 属性基测试的边界探索》
开发语言·python
勇往直前plus10 小时前
深入理解 Python 内存模型:模块、类、对象的存储与运行机制
开发语言·python
yunhuibin11 小时前
NIN网络学习
人工智能·python·深度学习·神经网络·学习