A
A
Alexander2019-05-22 21:49:33
Django
Alexander, 2019-05-22 21:49:33

How to force Django not to cache queryset in ListView?

Hello, there is a view, sales pages.

class SaleView(generic.ListView):
    template_name = 'artskill/sale.html'
    context_object_name = "products"
    queryset = [SaleImages.objects.filter(state=True), Product.objects.filter(show_sale_price=True)]

When I make a change to the database, the information on the sales page itself remains old, it changes only when the server is restarted. How to make django not cache this queryset?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-05-22
@AlexMine

What is caching? The class, together with its fields, is declared once - when the code is read by the interpreter. Accordingly, the value of the class field is defined in it until the end of the interpreter's work. If you want the value to be new each time it is accessed, use the method and return not a list, but a QuerySet:

class SaleView(generic.ListView):
    template_name = 'artskill/sale.html'
    context_object_name = "products"

    def get_queryset(self):
        return Product.objects.filter(show_sale_price=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question