# fastapi-upload **Repository Path**: yindj/fastapi-upload ## Basic Information - **Project Name**: fastapi-upload - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-20 - **Last Updated**: 2025-04-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Python 虚拟环境 - 创建虚拟环境:`python -m venv env` - 激活虚拟环境(windows):`env\Scripts\activate` - 激活虚拟环境(linux):`source env/bin/activate` - 退出虚拟环境:`deactivate` ### 安装依赖包 ```python pip install -U python-dotenv -i https://pypi.tuna.tsinghua.edu.cn/simple pip install -U fastapi -i https://pypi.tuna.tsinghua.edu.cn/simple pip install -U uvicorn[standard] -i https://pypi.tuna.tsinghua.edu.cn/simple pip install -U tortoise-orm[aiomysql] -i https://pypi.tuna.tsinghua.edu.cn/simple pip install -U cryptography -i https://pypi.tuna.tsinghua.edu.cn/simple ``` ### 创建数据库 ```python # 先进入数据库交互终端 mysql -uroot -p # 执行数据库创建语句 create database fastchat; # 创建管理账户,格式:CREATE USER '用户名'@'允许账号连接的主机地址' IDENTIFIED BY '密码'; CREATE USER 'fastchat'@'%' IDENTIFIED BY 'fastchat'; # 给新账户分配管理数据库的权限,格式:GRANT 管理权限 ON 数据库名.数据表名 TO '用户名'@'允许账号连接的主机地址'; GRANT ALL PRIVILEGES ON fastchat.* TO 'fastchat'@'%'; ``` ### 配置数据库连接 ```python # 打开.env文件,添加以下内容 DATABASE_URL=mysql://fastchat:fastchat@localhost:3306/fastchat ``` ### 运行项目 ```python # 进入项目根目录 cd fastchat # 激活虚拟环境 source env/bin/activate # 运行项目 uvicorn main:app --reload ``` ### restful api接口文档 http://127.0.0.1:8000/docs - 新增数据状态码:201 - 修改数据状态码:200 - 删除数据状态码:204 - 查询数据状态码:200 ### 数据迁移 - 安装依赖包:`pip install tortoise-orm[aiomysql]` - cd api/ - aerich init -t application.settings.TORTOISE_ORM - aerich init-db - aerich migrate - aerich upgradeww ### 数据模型 - 一对多关系:User 1--N Post - 用户1:文章 N ,通常会将关联关系维护在多个哪个模型中 ```python from tortoise import fields from tortoise.models import Model class User(Model): """用户表""" id = fields.IntField(pk=True) username = fields.CharField(max_length=50, unique=True) password = fields.CharField(max_length=100) nickname = fields.CharField(max_length=50) class Meta: table = 'users' table_description = '用户表' def __str__(self): return self.username class Post(Model): """文章表""" id = fields.IntField(pk=True) title = fields.CharField(max_length=100) content = fields.TextField() user = fields.ForeignKeyField('models.User', related_name='posts') class Meta: table = 'posts' table_description = '文章表' def __str__(self): return self.title ``` - 多对多关系:User 1--N Group, Group 1--N User ```python from tortoise import fields from tortoise.models import Model class User(Model): """用户表""" id = fields.IntField(pk=True) username = fields.CharField(max_length=50, unique=True) password = fields.CharField(max_length=100) nickname = fields.CharField(max_length=50) # groups = fields.ManyToManyField('models.Group', related_name='users') class Meta: table = 'users' table_description = '用户表' def __str__(self): return self.username class Group(Model): """用户组表""" id = fields.IntField(pk=True) name = fields.CharField(max_length=50, unique=True) users = fields.ManyToManyField('models.User', related_name='groups') class Meta: table = 'groups' table_description = '用户组表' def __str__(self): return self.name ``` - 一对一关系:User 1--1 Profile ```python from tortoise import fields from tortoise.models import Model class User(Model): """用户表""" id = fields.IntField(pk=True) username = fields.CharField(max_length=50, unique=True) password = fields.CharField(max_length=100) nickname = fields.CharField(max_length=50) // profile = fields.OneToOneField('models.Profile', related_name='user', null=True) class Meta: table = 'users' table_description = '用户表' def __str__(self): return self.username class Profile(Model): """用户信息表""" id = fields.IntField(pk=True) age = fields.IntField() address = fields.CharField(max_length=100) user = fields.OneToOneField('models.User', related_name='profile') class Meta: table = 'profiles' table_description = '用户信息表' def __str__(self): return self.address ``` ### jwt - 安装依赖包:`pip install jwt` - 安装python-jose:`pip install python-jose` ### websocket - 安装依赖包:`pip install websockets` -i https://pypi.tuna.tsinghua.edu.cn/simple