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

相关推荐
Destiny_where2 小时前
Agent平台-RAGFlow(2)-源码安装
python·ai
molunnnn2 小时前
第四章 Agent的几种经典范式
开发语言·python
linuxxx1104 小时前
django测试缓存命令的解读
python·缓存·django
weixin_537765804 小时前
【数据库管理】MySQL主从复制详解
数据库·mysql
毕设源码-邱学长5 小时前
【开题答辩全过程】以 基于Python的Bilibili平台数据分析与可视化实现为例,包含答辩的问题和答案
开发语言·python·数据分析
倚肆5 小时前
MySQL 配置文件属性详解
数据库·mysql
咚咚王者5 小时前
人工智能之编程进阶 Python高级:第十一章 过渡项目
开发语言·人工智能·python
A尘埃6 小时前
大模型应用python+Java后端+Vue前端的整合
java·前端·python
A尘埃6 小时前
LLM大模型评估攻略
开发语言·python