Opened 3 years ago

Closed 3 years ago

#20129 closed Bug (worksforme)

Storing a related queryset in attribute clears session

Reported by: francescortiz Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords:
Cc: pegler@…, bmispelon@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Hi, I don't know if I am missing something important, but I have a very weird behavior with this piece of code that took me some hours to find out:

class ModelA(models.Model, AbstractModel)

    def items():
            return self._items_qs_cache
        except AttributeError:
            current_minute = return, microsecond=0)
            self._items_qs_cache = self.children_set.filter(published=True, publish_date__lte=current_minute)
            return self_items_qs_cache

    parent = models.ForeignKey('self', blank=True, null=True, verbose_name=_('parent'), related_name='children_set')

Calling the items() function clears the session!

But, if instead you replace the following line

self._items_qs_cache = self.children_set.filter(published=True, publish_date__lte=current_minute)

with the following equivalent one

self._items_qs_cache = Model.objects.filter(published=True, publish_date__lte=current_minute, parent=self)

The session does not get cleared.

Is this a django bug or am I missing somthing.

Thank you,

Change History (2)

comment:1 Changed 3 years ago by pegler

  • Cc pegler@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

What session backend and database are you experiencing this with?


comment:2 Changed 3 years ago by bmispelon

  • Cc bmispelon@… added
  • Resolution set to worksforme
  • Status changed from new to closed


I've tried to reproduce this issue but I couldn't. Could you provide some more information on what is required to trigger this bug?

Here's the code I've used:

from django.db import models
from django.utils import timezone

class ModelA(models.Model):
    publish_date = models.DateTimeField(
    published = models.BooleanField(default=True)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children_set')

    def items(self):
            return self._items_qs_cache
        except AttributeError:
            current_minute =, microsecond=0)
            self._items_qs_cache = self.children_set.filter(published=True, publish_date__lte=current_minute)
            return self._items_qs_cache

from django.http import HttpResponse

from .models import ModelA

def counter(request):
    request.session['foo'] = request.session.get('foo', 0) + 1
    return HttpResponse("Current counter is at: %(foo)d" % request.session)

def clear_session(request):
    foo = Foo.objects.all()[0]
    return HttpResponse('done')

Accessing the "counter" view, you can see the counter increasing.
When I access the "clear_session" view, I get the done response as expected (no errors), and going back to the "counter" view shows that the counter has not been reset.

I marked the ticket as "worksforme", please reopen it if you have more information on how to reproduce it (like maybe the session backend you use).

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