tortoise / tortoise/tortoise-orm

Nested Prefetch with queryset and to_attr gets ignored

Open
#1,388 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
Nested Prefetch does not work

To Reproduce

await AModel.filter().prefetch_related(
    Prefetch("models", 
        queryset=BModel.filter().prefetch_related(
            Prefetch("cmodels", 
                queryset=CModels.filter(), to_attr=has_cmodels
            )
     )
) 

Expected behavior
the result contains the field has_cmodels

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the nested prefetch_related and Prefetch reproducer in the issue, focusing on the inner queryset and its to_attr value. Trace how nested prefetching handles the cmodels relation and verify whether the resulting objects expose has_cmodels. Done means the reproducer returns the expected field without breaking other prefetch behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.