Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get an old data set in one case after a GET request, and an up-to-date data set in the other

I make a POST request, enter the data into the table, but after that I do a Get and get the old data in this case

def get_queryset(self):
    transaction.commit()

    if self.action not in ['retrieve', 'list', 'create', 'selector']:
        res = self.queryset.filter(is_editable=True)
    else:
        res = self.queryset

    return res

Up-to-date data)

def get_queryset(self):
    transaction.commit()

    if self.action not in ['retrieve', 'list', 'create', 'selector']:
        res = self.queryset.filter(is_editable=True)
    else:
        res = Presence.objects.all()

    return res

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

QuerySets cache the value. That means that if you directly return self.queryset, it will run the query once, and store the values in memory.

If you add .all(), you make a copy of the QuerySet without the cache, and you thus force making a new query.

This is why a ListView by default has an implementation where it will return the queryset.all(). Indeed, if we look at the source code [GitHub], we see:

    def get_queryset(self):
        """
        Return the list of items for this view.
        The return value must be an iterable and may be an instance of
        `QuerySet` in which case `QuerySet` specific behavior will be enabled.
        """
        if self.queryset is not None:
            queryset = self.queryset
            if isinstance(queryset, QuerySet):
                queryset = queryset.all()
        elif self.model is not None:
            queryset = self.model._default_manager.all()
        else:
            raise ImproperlyConfigured(
                "%(cls)s is missing a QuerySet. Define "
                "%(cls)s.model, %(cls)s.queryset, or override "
                "%(cls)s.get_queryset()." % {
                    'cls': self.__class__.__name__
                }
            )
        ordering = self.get_ordering()
        if ordering:
            if isinstance(ordering, str):
                ordering = (ordering,)
            queryset = queryset.order_by(*ordering)

        return queryset
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading