Skip to content

Support relations in .only() - #1923

Merged
waketzheng merged 8 commits into
tortoise:developfrom
henadzit:feat/joins-in-only
Mar 27, 2025
Merged

waketzheng merged 8 commits into
tortoise:developfrom
henadzit:feat/joins-in-only

Conversation

@henadzit

@henadzit henadzit commented Mar 25, 2025 •

Copy link
Copy Markdown
Contributor

Description

  • .only supports selecting related fields, e.g. .only("related__field")
  • Add a check that .values(), .values_list() and .only() cannot be used together
  • Add expand_lookup_expression that can be used to convert the lookup expression (a__b__c) to a list of fields. Currently it is used in _join_select_related and _resolve_only but it can be reused in other places that support lookup expressions, for instance, in filters and ordering.

Motivation and Context

How Has This Been Tested?

make ci + a lot of new tests

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed

codspeed Bot commented Mar 25, 2025 •

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #1923 will not alter performance

Comparing henadzit:feat/joins-in-only (dcf9315) with develop (2aa49ad)

Summary

✅ 16 untouched benchmarks

@coveralls

coveralls commented Mar 26, 2025 •

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 14092795418

Details

  • 95 of 99 (95.96%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 89.66%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/backends/base/executor.py 3 4 75.0%
tortoise/queryset.py 59 60 98.33%
tortoise/query_utils.py 30 32 93.75%
Totals Coverage Status
Change from base Build 14051376405: 0.05%
Covered Lines: 6662
Relevant Lines: 7250

💛 - Coveralls

@henadzit henadzit changed the title WIP support relations in .only() Support relations in .only() Mar 26, 2025
@henadzit
henadzit marked this pull request as ready for review March 26, 2025 10:02
Comment thread tortoise/queryset.py
self.model,
(None,),
)
if append_item not in self._select_related_idx:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not needed since self._select_related_idx is always empty at this point.

@henadzit
henadzit requested review from abondar and waketzheng March 26, 2025 10:04
Comment thread tortoise/query_utils.py
fields.append(field)
model = field.related_model
try:
fields.append(model._meta.fields_map[field_names[-1]])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloud you leave some comment for why put the last element out of the for loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

Comment thread tortoise/backends/base/executor.py Outdated
if not any((v for _, v in related_items)):
obj = None
else:
if any((v for _, v in related_items)):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think

if any(v for _, v in related_items):

is more readable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if any(v for _, v in related_items): should be faster too because a new tuple isn't created. Updated!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, (v for _, v in related_items) is a generator.

@waketzheng
waketzheng self-requested a review March 27, 2025 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

如何同时使用only和select_related use only for nested model fields

3 participants