Django model manager queryset filter on aware datetime: no item display until server restart -


i have django model has publication_date , is_published fields. i've created manager model returns published items means: every item has is_published=true , publication_date <= now.

class publishedtextmanager(models.manager):     """         filters out unpublished items , items publication date in future     """     def get_query_set(self):         return super(publishedtextmanager, self).get_query_set() \             .filter(is_published=true) \             .filter(publication_date__lte=timezone.now()) 

the view that's using manager looks this:

class newsandeventsoverview(listview):      model = news     queryset = news.published.all().order_by('-publication_date')     context_object_name = 'news_list'      def get_context_data(self, **kwargs):         # initialize context , fill default data newsandeventsoverview super class         context = super(newsandeventsoverview, self).get_context_data(**kwargs)         # add view specific context         context['latest_news_item'] = context['news_list'][0]         today = timezone.now()         yesterday = today - timedelta(days=1)         context['upcoming_events_list'] = event.published.filter(q(date_end__gt=yesterday) | q(date_start__gt=yesterday)).order_by('date_start')         past_events_list = event.published.filter(q(date_end__lt=today) | q(date_start__lt=today)).order_by('-date_start')         old_news_list = context['news_list'][1:]         context['old_news_and_events_list'] = sorted(chain(old_news_list, past_events_list), key=lambda x: x.publication_date, reverse=true)         return context 

relevant urls.py:

from .views import newsandeventsoverview  urlpatterns = patterns('',     # index page     url(r'^$', newsandeventsoverview.as_view(), name="newsandevents_overview"), ) 

when add news item default receives current datetime (timezone.now()) publication date, when refresh page doesn't display in front-end until server restart (using django built-in server a.t.m). in amsterdam time (+2:00) , when add 2 hours publication_date filter works fine, since i'm new datetime awareness i'm guessing i'm doing wrong. i've tried timezone.now , without brackets, doesn't make difference.

i've been running similar issue, , here's think going on. when use queryset class attribute, query gets run on each request, timezone.now() call within manager not run on each request, @ class instantiation. try using get_queryset method instead, forces run on each request:

class newsandeventsoverview(listview):      model = news     context_object_name = 'news_list'      def get_queryset(self):         return news.published.all().order_by('-publication_date')      ... 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -