tortoise / tortoise/tortoise-orm
values_list enhancement - return related value as list, not as different list item
Nobody has claimed this yet.
- 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.
Let's consider following models and query
class User(Model):
id = fields.IntField(pk=True)
login = fields.CharField(max_length=32, null=False, unique=True)
roles = fields.ManyToManyField('models.Role', related_name="users", through="user_role")
class Role(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=64, null=False, unique=True)
Query:
users = await User.values_list("login", "roles__name")
It will return the list of {login, role__name}
[
{
"roles__name": "admin",
"login": "admin"
},
{
"roles__name": "string",
"login": "admin"
}
]
But the better option here is to return the list-in-list instead
[
{
"roles__name": ["admin", "string"],
"login": "admin"
}
]
Describe the solution you'd like
I would like to have here an flag to get values grouped, or just make it from default
[
{
"roles__name": ["admin", "string"],
"login": "admin"
}
]
Describe alternatives you've considered
Obviously it's possible to group it in code, just iterate on list, check if already in list, and append, but i would like to have it on tortoise base code instead
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 at the Python ORM's values_list implementation and its relation traversal for roles__name. Review how many-to-many results are assembled, then add focused coverage for grouped related values and clarify whether grouping is opt-in or the default. Done means the query returns one record per user with related role names collected in a list.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100