tortoise / tortoise/tortoise-orm

Support filtering with `relation__in` using model instances

Open
#1,819 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is your feature request related to a problem? Please describe.

In Django ORM, we can efficiently filter related objects using relation__in syntax, for example

Post.objects.filter(author__in=[user1, user2])

However, Tortoise ORM only supports relation_id__in, requiring us to extract IDs manually before filtering. This makes queries more verbose and less intuitive, especially when working with object instances rather than IDs.

Describe the solution you'd like

Add support for relation__in filtering in Tortoise ORM, allowing direct filtering using model instances similar to Django's implementation. For example:

await Post.filter(author__in=[user1, user2])  # Currently not supported
# Instead of current approach:
await Post.filter(author_id__in=[user1.id, user2.id])

Additional context

This feature would improve compatibility with Django ORM patterns.

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 at the Post.filter entry point and trace how the existing relation_id__in syntax is handled. Compare the desired relation__in examples with current filtering behavior, then add coverage showing that model instances can be passed directly and that existing ID-based filtering remains supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.