2010-04-12 25 views
6

這裏有一個奇怪的錯誤,也許有人能幫助追查來源,因爲它是試圖延長Django的CMS項目&嘗試使用使用寫了一些邏輯作爲項目,我不是完全清楚的一部分總之,使用:在意見泛型列表視圖提高屬性錯誤:「‘功能’對象有沒有屬性‘_clone’

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()引發錯誤w hether用作包裝函數的一部分,或者直接在urlpattern的queryset部分中使用。

一定是失去了一些東西明顯?認爲這是爲了使PublishedNewsManager不會將對象作爲字典返回,或者調整代碼以正確地將對象返回給視圖。

回答

9

_clone錯誤是由於將函數作爲參數傳遞給通用視圖而導致的一種紅鯡魚,其中QuerySet是預期的。將News.published.all()傳遞給通用視圖的代碼版本是正確的,因爲通用視圖將嘗試克隆它們提供的查詢集,以避免緩存他們查詢的第一批數據(因此在傳遞函數時會導致錯誤) 。

你的問題似乎是你的自定義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) 
+0

感謝insin,這只是門票! – Chris 2010-04-12 11:58:17

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", 
       )   
相關問題