这里有一个奇怪的错误,也许有人可以帮助追踪源代码,因为它正试图扩展Django CMS项目&试图使用一些逻辑作为该项目的一部分,我不完全清楚。简而言之,使用:
urls.py
======================
from django.conf.urls.defaults import *
from cmsplugin_flat_news.models import News
'''RETURNING _CLONE ERROR WHEN IMPLEMENTED
def get_news():
return News.published.all()
news_dict = {
'queryset': get_news,
}
news_list_dic = {
'queryset': get_news,
'paginate_by': 50,
}
'''
# NEXT SECTION FUNCTIONS BUT NEEDS SERVER RESTART TO SEE NEW POSTS.
#CHANGING TO JUST News.published.all RAISES SAME ISSUE AS USING WRAPPER
#SOLUTION ABOVE. SEE: http://docs.djangoproject.com/en/dev/topics/db/queries/#caching-and-querysets
#& EXAMPLE HERE: http://docs.djangoproject.com/en/dev/topics/generic-views/#adding-extra-context
news_dict = {
'queryset': News.published.all(),
}
news_list_dic = {
'queryset': News.published.all(),#SAME ISSUE
'paginate_by': 50,
}
urlpatterns = patterns('django.views.generic.list_detail',
(r'^$', 'object_list', news_list_dic),
(r'^(?P<page>[0-9]+)/$', 'object_list', dict(news_list_dic)),
url(r'^(?P<slug>[-\w]+)/$', 'object_detail', news_dict, name='news_view'),
)
models.py
======================
class PublishedNewsManager(models.Manager):
#Filters out all unpublished news and news with a publication date in the future
def get_query_set(self):
return super(PublishedNewsManager, self).get_query_set() \
.filter(is_published=True) \
.filter(pub_date__lte=datetime.datetime.now())
class News(models.Model):
title = models.CharField(_('Title'), max_length=255)
slug = models.SlugField(_('Slug'), unique_for_date='pub_date')
author = models.ForeignKey(User)
description = models.TextField(_('Description'), blank=True)
image = generic.GenericRelation('NewsImage', blank=True, null=True)
content = models.TextField(_('Content'), blank=True)
tags = TagField()
is_published = models.BooleanField(_('Published'), default=False)
pub_date = models.DateTimeField(_('Publication date'), default=datetime.datetime.now())
created = models.DateTimeField(auto_now_add=True, editable=False)
updated = models.DateTimeField(auto_now=True, editable=False)
published = PublishedNewsManager()
objects = models.Manager()
请参阅注释中的问题:基本上,通过实现'正确'解决方案为视图添加额外的上下文而引发的错误。错误是Attribute Error: "'function' object has no attribute '_clone'"
尝试:News.published.all而不是News.published.all()
引发错误,无论是作为包装函数的一部分还是直接在urlpattern的queryset部分中使用。
一定是缺少明显的东西吗?认为它是使用PublishedNewsManager不将对象作为字典返回,或者调整代码以正确地将对象返回到视图。
答案 0 :(得分:9)
_clone
错误是由于您将函数作为参数传递给期望QuerySet
的通用视图而导致的红色鲱鱼。将News.published.all()
传递给通用视图的代码版本是正确的,因为通用视图将尝试克隆它们所提供的QuerySet,以避免缓存它们查询的第一批数据(因此当您通过时会出错)在一个功能)。
您的问题似乎是您的自定义get_query_set
方法根据调用该方法的当前日期和时间返回QuerySet
进行过滤。
我在文档中看不到有关filter
参数可调用的内容,但我找到了this ticket which suggests that filter
can take callable arguments,所以请尝试更改您的经理以传递要调用的函数以获取当前日期/时间,而不是立即调用它:
class PublishedNewsManager(models.Manager):
def get_query_set(self):
return super(PublishedNewsManager, self).get_query_set() \
.filter(is_published=True) \
.filter(pub_date__lte=datetime.datetime.now)
答案 1 :(得分:0)
好吧,我并不是与OP完全相同的用例,但是我试图将RawQuerySet传递给泛型视图并获取_clone错误,我用一个丑陋的黑客修复了RawQuerySet实例。
通用视图想要做的唯一事情就是克隆查询集(用于缓存目的?),所以我让对象返回一个满足对_clone调用的自身副本。
错误:
'RawQuerySet'对象没有属性'_clone'
代码:
from django.views.generic.list_detail import object_list
....
li_file = File.objects.raw("SELECT * FROM file_file WHERE fn like %s", [search])
#adding this fixed the _clone missing error
def clone(self):
#return self - this works as well.
return copy.copy(self)
li_file.__class__._clone = clone
#end of addition
return object_list(request,
queryset = li_file,
template_name = "file/found.html",
)