tortoise / tortoise/tortoise-orm
prefetch_related无效
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
models.py
from datetime import datetime
from decimal import Decimal
from tortoise.models import Model
from tortoise import fields
from pydantic import BaseModel
class Tournament(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
created = fields.DatetimeField(auto_now_add=True)
def __str__(self):
return self.name
class ToItem(BaseModel):
name: str
class ToItemIn(ToItem):
pass
class ToItemOut(ToItem):
id: int
created: datetime
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
tournament = fields.ForeignKeyField(
'models.Tournament', related_name='events')
modified = fields.DatetimeField(auto_now=True)
prize = fields.DecimalField(max_digits=10, decimal_places=2, null=True)
def __str__(self):
return self.name
class EventItem(BaseModel):
name: str
prize: Decimal = None
tournament_id: int
class EventItemIn(EventItem):
pass
class EventItemOut(EventItem):
id: int
modified: datetime
api.py
from typing import Dict, List
from fastapi import APIRouter
from models import Tournament, Event, ToItemIn, EventItemIn,ToItemOut, EventItemOut
API = APIRouter(prefix='/api')
@API.post('/addtour', response_model=ToItemOut)
async def add_tour(item: ToItemIn):
tournament = Tournament(**item.dict())
await tournament.save()
return tournament
@API.get('/events')
async def get_eve():
events = await Tournament.first().prefetch_related('events')
return events
@API.post('/addevent',response_model=EventItemOut)
async def add_event(item: EventItemIn):
event = Event(**item.dict())
await event.save()
return event
main.py
from fastapi import FastAPI
import uvicorn
from tortoise.contrib.fastapi import register_tortoise
from api import API
import config
app = FastAPI()
register_tortoise(app=app,config=config.TORTOISE_ORM)
app.include_router(API)
if __name__ == '__main__':
uvicorn.run('main:app',reload=True)
config.py
TORTOISE_ORM = {
"connections": {"default": "mysql://root:zqs520....@127.0.0.1:3306/test"},
"apps": {
"models": {
"models": ["models", "aerich.models"],
"default_connection": "default",
},
},
"use_tz":False,
"timezone":"Asia/Shanghai"
}
问题描述
调用“/events”接口,没有正确返回Event数据
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with api.py's get_eve endpoint and the Tournament.events relation in models.py. Run /events using the MySQL configuration in config.py and compare its response with the related Event records. Done means the endpoint correctly returns the expected Event data through prefetch_related.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, mysql, python
- Domain
- api, backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100