tortoise / tortoise/tortoise-orm
How to prefetch only the primary keys for a Many to Many Relation?
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
class Team(Model):
id = fields.IntField(pk=True)
events: fields.ManyToManyRelation[Event]
class Event(Model):
id = fields.IntField(pk=True)
participants = fields.ManyToManyField('models.Team', related_name='events', through='event_team')
How do I correctly prefetch so I can access the primary keys of the ManyToMany relation?
I have models with binary blobs and I'd like to only return the related IDs over an API.
I expected this to work but unfortunately it doesn't
t = await Team.get(pk=1).first().prefetch_related('events__id'')
# I'd like to just have the PKs of the relations
for event_id in t.events__id:
print(event_id)
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 the prefetch_related entry point and the ManyToMany relation-loading documentation. Determine whether related primary keys can be prefetched without loading binary fields, and document the supported usage or limitation for accessing those IDs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- database
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100