Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #28344 -- Allowed customizing queryset in Model.refresh_from_db()/arefresh_from_db(). #17592

Merged
merged 1 commit into from Jan 15, 2024

Conversation

aivarsk
Copy link
Contributor

@aivarsk aivarsk commented Dec 10, 2023

The from_queryset parameter can be used to:

  • use a custom Manager supporting soft delete:
    from_queryset=Model.active_objects.all()
  • lock the row until the end of transaction:
    from_queryset=Model.objects.select_for_update()
  • select additional related objects
    from_queryset=Model.objects.select_related('related_field')

@aivarsk aivarsk changed the title Fixed #28344 -- Added for_update parameter to Model.refresh_from_db() Fixed #28344 -- Added from_queryset parameter to Model.refresh_from_db() Dec 12, 2023
Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

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

@aivarsk Thanks for this patch 👍 I left initial comments.

docs/ref/models/instances.txt Show resolved Hide resolved
docs/ref/models/instances.txt Show resolved Hide resolved
docs/releases/5.1.txt Outdated Show resolved Hide resolved
docs/ref/models/instances.txt Outdated Show resolved Hide resolved
docs/ref/models/instances.txt Outdated Show resolved Hide resolved
docs/releases/5.1.txt Outdated Show resolved Hide resolved
django/db/models/base.py Outdated Show resolved Hide resolved
Copy link
Member

@charettes charettes left a comment

Choose a reason for hiding this comment

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

Thank you for committing to this patch @aivarsk, this is looking great!

I don't have anything to add really, the fact select_related is also tested and that the documentation includes examples of how it can be relied on to exclude rows and lock them. Seing the patch completed reinforces my believe that this is going to be useful for more than just row locking like it was intentionally intended.

Approving assuming the release note message is adjusted.

tests/basic/tests.py Outdated Show resolved Hide resolved
docs/releases/5.1.txt Outdated Show resolved Hide resolved
@felixxm felixxm self-assigned this Jan 14, 2024
@felixxm felixxm changed the title Fixed #28344 -- Added from_queryset parameter to Model.refresh_from_db() Fixed #28344 -- Allowed customizing queryset in Model.refresh_from_db()/arefresh_from_db(). Jan 15, 2024
…()/arefresh_from_db().

The from_queryset parameter can be used to:
- use a custom Manager
- lock the row until the end of transaction
- select additional related objects
@felixxm
Copy link
Member

felixxm commented Jan 15, 2024

@aivarsk Thanks for this patch 👍 Welcome aboard ⛵

I added support for arefresh_from_db() and made small edits to docs.

@felixxm felixxm merged commit f92641a into django:main Jan 15, 2024
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants