2

I am having an odd issue. This is my view:


def get_date_range(this_week=True, last_week=False, older_than_two_weeks=False):

    dates = {}
    d = datetime.today().strftime('%Y-%m-%d')
    dt = datetime.strptime(d, '%Y-%m-%d')
    if this_week:
        this_week_start = dt - timedelta(days = dt.weekday())
        this_week_end = this_week_start + timedelta(days=6)
    elif (last_week or older_than_two_weeks):
        this_week_start = dt - timedelta(days = 14)
        this_week_end = this_week_start + timedelta(days=6)

    dates = {'date_start' :this_week_start, 'date_end' : this_week_end}
    return dates

def load_created_topics_by_time(request):
    """
    created this week, last week, older
    """
    objects={}

    date_range = get_date_range(this_week=True)
    topics_this_week = Topic.objects
                       .filter(is_active=True, date_created__range=(date_range['date_start'],date_range['date_end']))
                       .order_by('-date_created')

    objects['topics_this_week'] = topics_this_week

    date_range = get_date_range(this_week=False,last_week=True)
    topics_last_week = Topic.objects.filter(is_active=True, date_created__range= (date_range['date_start'],date_range['date_end'])).order_by('-date_created')

    objects['topics_last_week'] = topics_last_week

    topics_older_two_weeks = Topic.objects.filter(is_active=True, date_created__lt=date_range['date_start']).order_by('-date_created')[:50]

    objects['topics_older_two_weeks'] = topics_older_two_weeks

    return shortcuts.render(request, 'template.html', objects)

Now when i run it on my machine it works perfectly, but loaded from the server apparently hits the "get_date_range" only once. It seems like the result of that function is getting cached and that, of course, messes up my results.

I can't use @never_cache because the its a function and not a view. Any solutions?

Thanks, David

Dave
  • 43
  • 5
  • Is that really your exact code? You're sure you are calling the get_date_range function from within the load_created_topics_by_time view? – Daniel Roseman Apr 13 '15 at 20:40
  • Yes, its the exact code. both calls to the function are within load_created_topics_by_time – Dave Apr 13 '15 at 20:46
  • Why can't you use `@never_cache`? The view is being cashed which calls the function, thus the function is only called once. – Wallace Apr 13 '15 at 21:23
  • I already tried using that decorator on load_created_topics_by_time view, but still no luck. – Dave Apr 13 '15 at 21:26
  • Could this be a data issue? The data exists on your local, but not on the server? – vishen Apr 14 '15 at 00:19
  • No, if i modify the view and calculate the different date ranges without calling rhe function the data is retrieved correctly. This is really bugging me... – Dave Apr 14 '15 at 00:33

1 Answers1

0

I ran into the exact same kind of problem and it drove me mad!

It is (or at least was for me) a caching problem at the QuerySet level. After days of trial and error, I finally tackled it down by using generic class-based views and doing the "date-dependent" logic inside the get_queryset method of a custom model Manager (https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers).

Also, I'm not sure if it is because I "learned my lesson" or it is related to the switch but it seems the problem appears less often since I've switched from apache + mod_wsgi to nginx + hand-compiled uWSGI (the fact it is hand-compiled is not the point, it just means I'm using a recent version, not the one supplied by my distribution).

Hope this answer will be helpfull.

Emma
  • 1,061
  • 9
  • 10