Opened 12 months ago

Closed 9 months ago

Last modified 9 months ago

#22553 closed New feature (fixed)

Add ability to refresh queryset

Reported by: giggaflop@… Owned by: dhoffman
Component: Documentation Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I propose adding a Queryset method '.refresh()' which will clear the built-in cache on any queryset passed to it.

I suggest an initial implementation of:

    def refresh(self, *args, **kwargs):
        """
        Returns a new QuerySet instance with the args ANDed to the existing
        set.
        """
        self._result_cache = None
        return self

Reason this is requested:

When I'm using modelformsets I typically find the need to refresh the queryset that is used by the ModelFormset.

To do this I perform the following action:

        updated_formset = ConciergeUserPoolFormset(request.POST)
        if updated_formset.is_valid():
            updated_formset.save()
            queryset = updated_formset.get_queryset()
            # we need an updated queryset, not the cached version. set the results cache to None to force re-fetch
            queryset._result_cache = None
            updated_formset = ConciergeUserPoolFormset(queryset=queryset)

This is not an obvious solution to the issue of updating a ModelFormset or forcing a queryset update in general, it also uses a private method of the Queryset object which I would prefer to avoid.

Change History (11)

comment:1 Changed 12 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Maybe _clone() already does what you need.

comment:2 Changed 10 months ago by timo

  • Triage Stage changed from Unreviewed to Accepted

I guess we probably don't want to document an underscore method. I wonder if there is any reason not to rename it (with deprecation) to clone() or something similar?

comment:3 Changed 10 months ago by akaariai

  • Component changed from Database layer (models, ORM) to Documentation
  • Easy pickings set

.all() can be used for this. Seems like this isn't documented, so lets do that. The right place seems to be https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.all.

comment:4 Changed 10 months ago by nikisweeting@…

I'd be happy to write a little blurb in the docs showing how to use .all() for refreshing a QuerySet. I'm new to contributing to here though, how do I go about submitting my proposed documentation amendment?

comment:5 Changed 10 months ago by timo

Fortunately, we have extensive docs that explains how to contribute. :-)

comment:6 Changed 9 months ago by dhoffman

  • Owner changed from nobody to dhoffman
  • Status changed from new to assigned

comment:8 Changed 9 months ago by timo

  • Has patch set
  • Triage Stage changed from Accepted to Ready for checkin

Looks good. Don't forget to check "Has patch" so the ticket appears in the review queue. Thanks!

comment:9 Changed 9 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 6d5daa30cf29d0bada0586213ecb2959152566b4:

Fixed #22553 -- Added refreshing queryset info to docs.

comment:10 Changed 9 months ago by Tim Graham <timograham@…>

In e3a99357cba5085f5ae937920529292edafc5c7a:

[1.6.x] Fixed #22553 -- Added refreshing queryset info to docs.

Backport of 6d5daa30cf from master

comment:11 Changed 9 months ago by Tim Graham <timograham@…>

In 1ef544f91f0d4be3f44ae7fd0498530e57a9b160:

[1.7.x] Fixed #22553 -- Added refreshing queryset info to docs.

Backport of 6d5daa30cf from master

Note: See TracTickets for help on using tickets.
Back to Top