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

相关推荐
Dxy12393102161 分钟前
Python经典算法实战
开发语言·python·算法
农民也会写代码16 分钟前
织梦dedecms上传附件不自动改名的办法
mysql·php·dedecms
谷晓光34 分钟前
在PyCharm中使用pyenv指定的Python:配置指南
ide·python·pycharm
猴子请来的逗比4892 小时前
事务操作语句
学习·mysql
Algorithm15762 小时前
掌握聚合函数:COUNT,MAX,MIN,SUM,AVG,GROUP BY和HAVING子句的用法,Where和HAVING的区别
mysql
love530love2 小时前
【笔记】为 Miniconda 安装图形界面的方法
人工智能·windows·笔记·python·conda
五步晦暝3 小时前
【Python 集合 Set 】全面学习指南
开发语言·python
yorushika_5 小时前
python打卡训练营打卡记录day35
python·深度学习·机器学习·超参数
星释7 小时前
Mac Python 安装依赖出错 error: externally-managed-environment
开发语言·python·macos
苹果酱05677 小时前
Java设计模式:探索编程背后的哲学
java·vue.js·spring boot·mysql·课程设计