我正在尝试使用 orm for ORM 在 fastAPI 中制作记分牌 API。我遇到了一些错误,我完全不知道为什么,因为我的 Score 类有属性日期。
主要.py:
from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise
from scoreboard.models import (Score, ScoreIn_Pydantic, Team, Team_Pydantic,
TeamIn_Pydantic)
app = FastAPI()
db: list[Team] = []
register_tortoise(app=app,
db_url="sqlite://db.sqlite3",
modules={"models": ["scoreboard.models"]},
generate_schemas=True,
add_exception_handlers=True)
@app.get("/")
async def root():
return True
@app.get("/score")
async def get_score() -> list[Team_Pydantic]:
return await Team_Pydantic.from_queryset(Team.all())
@app.get("/score/{team_name}")
async def get_team_score(team_name: str):
return Team_Pydantic.from_queryset_single(Team.get(name=team_name))
@app.put("/score")
async def post_score(team: TeamIn_Pydantic, score: ScoreIn_Pydantic):
team_ = await Team.filter(name=team.dict()["name"]).first()
if team_ is None:
team_ = await Team.create(**team.dict(), **score.dict())
else:
await team_.score_history.add(Score(**score.dict()))
await team_.save()
return await Team_Pydantic.from_tortoise_orm(team_)
模型.py:
from datetime import date as dt
from tortoise import fields
from tortoise.contrib.pydantic.creator import pydantic_model_creator
from tortoise.models import Model
class Score(Model):
id: int = fields.IntField(pk=True)
score: int = fields.IntField()
date: dt = fields.DateField()
def __str__(self):
return f"{self.today_date()}: {self.score}"
def today_date(self):
return dt.today()
class PydanticMeta:
computed = ['date']
class Team(Model):
id: int = fields.IntField(pk=True)
name: str = fields.CharField(max_length=20, index=True)
total_score: int = fields.IntField()
score_history = fields.ManyToManyField("models.Score",
related_name='score_history')
def calculate_total_score(self):
return self.score_history.all().aggregate(Sum('score'))
def __str__(self):
return f"{self.name}: {self.total_score}"
class PydanticMeta:
computed = ['total_score']
Score_Pydantic = pydantic_model_creator(Score, name="Score")
Team_Pydantic = pydantic_model_creator(Team, name="Team")
ScoreIn_Pydantic = pydantic_model_creator(Score,
name="Score",
exclude_readonly=True)
TeamIn_Pydantic = pydantic_model_creator(Team,
name="TeamIn",
exclude_readonly=True)
错误:
AttributeError: type object 'Score' has no attribute 'date'
完整追溯:
> uvicorn scoreboard.main:app
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Program Files\Python39\Scripts\uvicorn.exe\__main__.py", line 7, in <module>
server.run()
File "C:\Program Files\Python39\lib\site-packages\uvicorn\server.py", line 49, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "C:\Program Files\Python39\lib\site-packages\uvicorn\server.py", line 56, in serve
config.load()
File "C:\Program Files\Python39\lib\site-packages\uvicorn\config.py", line 308, in load
self.loaded_app = import_from_string(self.app)
File "C:\Program Files\Python39\lib\site-packages\uvicorn\importer.py", line 20, in import_from_string
module = importlib.import_module(module_str)
File "C:\Program Files\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File ".\scoreboard\main.py", line 3, in <module>
from scoreboard.models import Score, Team, Team_Pydantic, TeamIn_Pydantic, ScoreIn_Pydantic
File ".\scoreboard\models.py", line 41, in <module>
Score_Pydantic = pydantic_model_creator(Score, name="Score")
File "C:\Users\navdh\AppData\Roaming\Python\Python39\site-packages\tortoise\contrib\pydantic\creator.py", line 271, in pydantic_model_creator
{
File "C:\Users\navdh\AppData\Roaming\Python\Python39\site-packages\tortoise\contrib\pydantic\creator.py", line 272, in <dictcomp>
k: {"field_type": callable, "function": getattr(cls, k), "description": None}
AttributeError: type object 'Score' has no attribute 'date'