Opened 12 years ago
Closed 12 years ago
#20129 closed Bug (worksforme)
Storing a related queryset in attribute clears session
Reported by: | Francesc Ortiz | 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 |
Description
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(): try: return self._items_qs_cache except AttributeError: current_minute = return datetime.now().replace(second=0, 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 by , 12 years ago
Cc: | added |
---|
comment:2 by , 12 years ago
Cc: | added |
---|---|
Resolution: | → worksforme |
Status: | new → closed |
Hi,
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:
# models.py from django.db import models from django.utils import timezone class ModelA(models.Model): publish_date = models.DateTimeField(default=timezone.now) published = models.BooleanField(default=True) parent = models.ForeignKey('self', blank=True, null=True, related_name='children_set') def items(self): try: return self._items_qs_cache except AttributeError: current_minute = timezone.now().replace(second=0, microsecond=0) self._items_qs_cache = self.children_set.filter(published=True, publish_date__lte=current_minute) return self._items_qs_cache # views.py 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] foo.items() 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).
What session backend and database are you experiencing this with?
Best,
Matt