2012-08-28 98 views
0

很簡單的代碼,但一個奇怪的情況...「DoesNotExist:ContentType的匹配查詢不存在」只發生偶爾

有這個片段的模板:

{% is_contest story as iscontest %} 

其中is_contest這是自定義模板標籤:

@register.assignment_tag 
def is_contest(obj): 
    contesttype = ContentType.objects.get_for_model(Contest) 
    return obj.source_content_type == contesttype 

但是比賽是在django_content_type表中。

最古怪的部分:這只是一個間歇性的錯誤,而且似乎只是由模板標籤引起的(我也是在視圖中檢查這個選項,但是沒有錯誤)。

任何想法?

編輯:全回溯

Traceback (most recent call last): 

    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response 
    response = callback(request, *callback_args, **callback_kwargs) 

    File "/home/ubuntu/src/cbframe/commentableobjects/views/commentableobject_list.py", line 142, in commentablobject_list 
    return render_to_response('commentableobjects-list.html', info, context_instance=RequestContext(request)) 

    File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py", line 20, in render_to_response 
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 176, in render_to_string 
    return t.render(context_instance) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render 
    return self._render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render 
    return self.nodelist.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render 
    bit = self.render_node(node, context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node 
    return node.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 123, in render 
    return compiled_parent._render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render 
    return self.nodelist.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render 
    bit = self.render_node(node, context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node 
    return node.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 62, in render 
    result = block.nodelist.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render 
    bit = self.render_node(node, context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node 
    return node.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 281, in render 
    return nodelist.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render 
    bit = self.render_node(node, context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node 
    return node.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 155, in render 
    return self.render_template(self.template, context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 137, in render_template 
    output = template.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render 
    return self._render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render 
    return self.nodelist.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render 
    bit = self.render_node(node, context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node 
    return node.render(context) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 185, in render 
    nodelist.append(node.render(context)) 

    File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1139, in render 
    context[self.target_var] = func(*resolved_args, **resolved_kwargs) 

    File "/home/ubuntu/src/cbframe/commentableobjects/templatetags/url_target_blank.py", line 27, in is_contest 
    return obj.source_content_type == contesttype 

    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 350, in __get__ 
    rel_obj = qs.get(**params) 

    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 366, in get 
    % self.model._meta.object_name) 

DoesNotExist: ContentType matching query does not exist. 

回答

1

一個問題可能是您比較2個實例與海誓山盟,而這些應該是在Django相同的,因爲Django的緩存他們,這不是一個保證。

不知道如何/在哪裏的source_content_type從何而來,但我會建議這個:

return isinstance(obj, contesttype) 

至於原來的問題,你有一個堆棧跟蹤?這個錯誤似乎有點奇怪。

你確定Contest沒有被覆蓋在範圍的某個地方嗎? Contest可能被模塊中的其他代碼覆蓋,那麼最有可能的嫌疑人將是global Contest,但在這裏似乎並不是這樣。

由於堆棧跟蹤問題有點清晰。

這部分明確表明,它會錯在obj.source_content_type部分:

return obj.source_content_type == contesttype 

我覺得有些對象不具有現有source_content_type了。

+0

source_content_type是obj的一個字段(即....對象源的內容類型...看到了嗎?)。所以我沒有比較這兩個對象,我正在比較一個領域和另一個領域。比賽將如何被覆蓋?這是一個簡單的模型導入。 – Colleen

+0

感謝堆棧跟蹤@Colleen,現在我知道問題是什麼:) 它實際上並不是找不到的'Contest'對象,而是'source_content_type'。它使用的是一種顯然不存在的類型(已經存在)。 – Wolph

+0

我假定查詢直到下一行才被評估,因爲在Django中查詢延遲評估直到必要時纔是常見的。對於source_content_type,默認值設置爲0,一個對象被創建爲錯誤,並獲得了不存在的默認值。 – Colleen