tortoise-orm 生成Pydantic,丢失外键字段
目录结构大致如下
models.py
class Project(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=255, description="项目名称", unique=True)
desc = fields.TextField(description="项目描述", null=True)
created_at = fields.DatetimeField(auto_now_add=True)
modified_at = fields.DatetimeField(auto_now=True)
class Env(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=255, description="环境名称")
base_url = fields.CharField(255, description="基准地址")
base_header = fields.TextField(description="基准请求头", null=True)
db_type = fields.CharField(255, description="数据库类型", null=True)
db_host = fields.CharField(255, description="数据库地址", null=True)
db_port = fields.CharField(255, description="数据库端口", null=True)
db_name = fields.CharField(255, description="数据库名称", null=True)
db_charset = fields.CharField(255, description="数据库编码", null=True)
db_user = fields.CharField(255, description="数据库用户", null=True)
db_passwd = fields.CharField(255, description="数据库密码", null=True)
# 外键关联project表,可通过.envs访问到项目所有表 https://tortoise-orm.readthedocs.io/en/latest/models.html
project = fields.ForeignKeyField('models.Project', related_name='envs')
# 生成pydantic 模型
Project_Pydantic = pydantic_model_creator(Project, name="Project")
ProjectIn_Pydantic = pydantic_model_creator(Project, name="ProjectIn", exclude_readonly=True)
Env_Pydantic = pydantic_model_creator(Env, name="Env")
EnvIn_Pydantic = pydantic_model_creator(Env, name="EvnIn", exclude_readonly=True)
routers.py
#!/usr/bin/env/ python3
# -*- coding:utf-8 -*-
"""
@Project: apiAutoTestFastAPI
@File :env.py
@Author:zy7y
@Date :2021/4/19 21:18
@Desc :
"""
from fastapi import APIRouter
from db import models
envs = APIRouter(tags=["环境相关"])
@envs.post("/env")
async def create(env: models.EnvIn_Pydantic):
return await models.Env.all()
swaager
解决
tip: 在生成Pydantic模型前 使用 Tortoise.init_models(["db.models"], "models")
其中
db.models
为模块路径
models.py
"""
project: apiAutoTestWeb
file: models.py
author: zy7y
date: 2021/4/17
"""
from tortoise import fields, Tortoise
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.models import Model
class Project(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=255, description="项目名称", unique=True)
desc = fields.TextField(description="项目描述", null=True)
created_at = fields.DatetimeField(auto_now_add=True)
modified_at = fields.DatetimeField(auto_now=True)
class Env(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=255, description="环境名称")
base_url = fields.CharField(255, description="基准地址")
base_header = fields.TextField(description="基准请求头", null=True)
db_type = fields.CharField(255, description="数据库类型", null=True)
db_host = fields.CharField(255, description="数据库地址", null=True)
db_port = fields.CharField(255, description="数据库端口", null=True)
db_name = fields.CharField(255, description="数据库名称", null=True)
db_charset = fields.CharField(255, description="数据库编码", null=True)
db_user = fields.CharField(255, description="数据库用户", null=True)
db_passwd = fields.CharField(255, description="数据库密码", null=True)
# 外键关联project表,可通过.envs访问到项目所有表 https://tortoise-orm.readthedocs.io/en/latest/models.html
project = fields.ForeignKeyField('models.Project', related_name='envs')
Tortoise.init_models(["db.models"], "models")
Project_Pydantic = pydantic_model_creator(Project, name="Project")
ProjectIn_Pydantic = pydantic_model_creator(Project, name="ProjectIn", exclude_readonly=True)
Env_Pydantic = pydantic_model_creator(Env, name="Env")
EnvIn_Pydantic = pydantic_model_creator(Env, name="EvnIn", exclude_readonly=True)
参考资料
https://github.com/tortoise/tortoise-orm/issues/543