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

相关推荐
用户83562907805117 分钟前
Python 实现 Excel 条件格式自动化
后端·python
深蓝电商API1 小时前
Scrapy管道Pipeline深度解析:多方式数据持久化
爬虫·python·scrapy
噎住佩奇1 小时前
(Win11系统)搭建Python爬虫环境
爬虫·python
basketball6161 小时前
python 的对象序列化
开发语言·python
rgeshfgreh2 小时前
Python流程控制:从条件到循环实战
前端·数据库·python
luoluoal2 小时前
基于python大数据的电影市场预测分析(源码+文档)
python·mysql·django·毕业设计·源码
conca2 小时前
Java+MySQL时区难题-Date自动转换String差8小时
数据库·mysql
幻云20102 小时前
Python深度学习:从入门到实战
人工智能·python
壹米饭2 小时前
MYSQL进阶:删除视图时视图被lock解决方案
后端·mysql
程序设计实验室2 小时前
不同Django服务器和部署方式的性能调研
django