2013-12-19 54 views
3

我一直在拉我的頭髮,這似乎有一個非常簡單的解決方案,但我太盲目,看不到它。 我已經從Django 1.4.3升級到Django 1.6,並且從那時起我試圖讓DateTimeField工作時出現斷言錯誤。django中的AssertionError

這裏是我的模型

class Article(models.Model): 
'''Article Model''' 

banner = models.ImageField(verbose_name="Banner", null=True, blank=True, upload_to='ajax_uploads/banners', max_length=300) 

title = models.CharField(
    verbose_name = _(u'Title'), 
    help_text = _(u' '), 
    max_length = 255 
) 
slug = models.SlugField(
    verbose_name = _(u'Slug'), 
    help_text = _(u'Uri identifier.'), 
    max_length = 255 
) 
content_markdown = models.TextField(
    verbose_name = _(u'Content (Markup)'), 
    help_text = _(u' '), 
) 
content_markup = models.TextField(
    verbose_name = _(u'Content (Markup)'), 
    help_text = _(u' '), 
) 
categories = models.ManyToManyField(
    Category, 
    verbose_name = _(u'Categories'), 
    help_text = _(u' '), 
    null = True, 
    blank = True 
) 
date_publish = models.DateTimeField(
    default=datetime.date.today, 
    verbose_name = _(u'Publish Date'), 
    help_text = _(u' ') 
) 

class Meta: 
    app_label = _(u'blog') 
    verbose_name = _(u'Article') 
    verbose_name_plural = (u'Articles') 
    ordering = ['-date_publish'] 

def save(self): 
    self.content_markup = markdown(self.content_markdown, ['codehilite']) 
    super(Article, self).save() 

def __unicode__(self): 
     return '%s' % (self.title,) 

views.py:

def index(request): 
'''News index''' 
archive_dates = Article.objects.dates('date_publish','month', order='DESC') 
categories = Category.objects.all() 

page = request.GET.get('page') 
article_queryset = Article.objects.all() 
paginator = Paginator(article_queryset, 5) 

try: 
    articles = paginator.page(page) 
except PageNotAnInteger: 
    #If page requested is not an integer, deliver first page. 
    articles = paginator.page(1) 
except EmptyPage: 
    #If page requested is out of range, deliver last page of results. 
    articles = paginator.page(paginator.num_pages) 

return render(
    request, 
    'blog/article/index.html', 
{ 
    'articles' : articles, 
    'archive_dates' : archive_dates, 
    'categories' : categories 
} 
) 

和模板

     <div class="8u skel-cell-important"> 
            {% for item in articles %} 
              <!-- Content --> 
               <article class="box is-post"> 
                <a href="{% url "blog-article-single" slug=item.slug %}" class="image image-full"><img src="/media/{{ item.banner }}" alt="" /></a> 
                <header> 
                 <h2><a href="{% url "blog-article-single" slug=item.slug %}">{{ item.title }}</a></h2> 
                 <span class="byline">Published {{ item.date_publish|date:"j, M, Y" }}</span> 
                </header> 
                <p> 
                 {{ item.content_markup|safe|slice:":250" }}... 
                </p> 
               </article> 
            {% endfor %} 
             </div> 

最後錯誤追蹤:

AssertionError at /blog/ 

'date_publish' is a DateTimeField, not a DateField. 

Request Method:  GET 
Request URL: http://localhost:8000/blog/ 
Django Version:  1.6.1 
Exception Type:  AssertionError 
Exception Value:  

'date_publish' is a DateTimeField, not a DateField. 

Exception Location:  /usr/local/lib/python2.7/dist-packages/django/db/models/sql/subqueries.py in _check_field, line 258 
Python Executable: /usr/bin/python2.7 
Python Version:  2.7.3 
Python Path:  

['/home/user/paperpxl', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PIL', 
'/usr/lib/python2.7/dist-packages/gst-0.10', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/pymodules/python2.7', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client', 
'/usr/lib/python2.7/dist-packages/ubuntuone-client', 
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', 
'/usr/lib/python2.7/dist-packages/ubuntuone-couch', 
'/usr/lib/python2.7/dist-packages/ubuntuone-installer', 
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol'] 

Server time: Thu, 19 Dec 2013 15:28:43 +0000 

任何形式的幫助,將不勝感激!

編輯:感謝你們的幫助,我會回到發行說明再次閱讀它們!我希望我喜歡你的細節。

+0

用於拉出你的頭髮的+1 – JoeC

+0

在做Django升級時,值得花時間按部就班地完成它們。即使它是一個相對較小/未發佈的項目。例如,首先升級到最新的1.5版本。然後,如果有什麼東西從那裏升級到1.6,你可以縮小到「好吧,這與1.6版本有關」 – ptr

+0

我只意識到當我調試這個錯誤時,我的django版本改變了。看來我使用的一個應用程序卸載了1.4.3並且安裝了1.6。 從現在開始,我會密切關注我的日誌。謝謝你的提示! – Hevlastka

回答

7

date_publishDateTimeField而不是DateField。使用datetimes()方法而不是dates()

Article.objects.datetimes('date_publish', 'month', order='DESC') 

有關詳細信息,請參閱1.6 release notes

1

Django 1.6 release notes

QuerySet.dates()不再可用上DateTimeField字段

QuerySet.dates()引發,如果它的上DateTimeField字段一起使用時 時區支持激活的錯誤。改用QuerySet.datetimes()。

您在視圖上使用dates,使用date_publish DateTimeField模型。