On May 11, 7:08 pm, Malcolm Box <malcolm....@gmail.com> wrote:
> Hi,
>
> I've run into a weird bug that has me scratching my head - I'm seeing
> inconsistent results from a queryset between various apache processes and
> the django shell.  I.e. sometimes a GET gives me the right response (as it
> hits a "correct" Apache instance) and other times it gives a wrong
> response.  Restarting Apache causes problem to go away, but it resurfaces
> later.
>
> I have a Poll class which has a start_time and an end_time, both of which
> are optional, plus an enabled field.  A poll is considered active if it's
> enabled, and if it has a start_time then now > start_time, and if it has an
> end_time then now < end_time.
>
> What I'm seeing is that sometimes polls with a start_time that is before now
> are not showing up in the responses when I do a series of GETs.  So e.g.
> first GET - shows poll, second one - doesn't show, third - doesn't, fourth -
> does and so on.
>
> From a fresh python shell, the Poll.objects.active() call always gives the
> right results.
>
> The code in question is:
>
> class PollManager(models.Manager):
>     def active(self):
>         qs = super(PollManager, self).get_query_set().filter(enabled=True)
>         q_start = Q(start_time__isnull=True) |
> Q(start_time__lte=datetime.datetime.now())
>         q_end = Q(end_time__isnull=True) |
> Q(end_time__gte=datetime.datetime.now())
>         return qs.filter(q_start,q_end)
>
> Where the models are is:
>
> class Event(models.Model):
>        enabled = models.BooleanField() # automatic default false
>        .... other fields
>
> class Poll(models.Model):
>     event = models.ForeignKey(Event, db_index=True)
>     enabled = models.BooleanField() # automatic default false
>     start_time = models.DateTimeField(blank=True, null=True, help_text="Time
> to enable this poll (optional)")
>     end_time = models.DateTimeField(blank=True, null=True, help_text="Time
> to close this poll (optional)")
>
>     objects = PollManager()
>
> The view logic is basically:
>
> def view_polls(request, event_id):
>         filtered_set =  self.queryset._clone()    # Queryset is
> Poll.objects.active().filter(event__enabled = True)
>         filtered_set = filtered_set.filter(event__id=event_id)
>         return render_to_response(template, filtered_set)
>
> (this has been elided and adapted - original code is using django-rest-api,
> JSON responder etc.  But the call boils down to this)
>
> I'm confused as to what the problem could be - any clues?
>
> Cheers,
>
> Malcolm

I'd like to see how the queryset is assigned to `self`. My suspicion
would be that the arguments are being preserved across requests - when
it's first instantiated, it correctly uses the current time to filter,
but on subsequent calls it is still using the time as of the first
request.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to