mongoengine mongodbforms throung意外错误AttributeError:' object'对象没有属性' __ getitem __'

时间:2015-03-20 12:41:35

标签: python django mongodb django-queryset mongoengine

我正在使用django mongoengine我收到此错误我搜索了google和stackoverflow但没有得到任何解决方案这是我得到的全新类型的错误。

这是我的追踪:

Internal Server Error: /quixom/album-view/
Traceback (most recent call last):
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 113, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/cis/DjangoLive/quixom/quixom/apps/users/views.py", line 102, in album_view
    return render(request, 'photologue/album_view.html', ctx)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 53, in render
    return HttpResponse(loader.render_to_string(*args, **kwargs),
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/loader.py", line 177, in render_to_string
    return t.render(context_instance)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
    return compiled_parent._render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 284, in render
    if match:
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 47, in __len__
    list(self._iter_results())
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 81, in _iter_results
    self._populate_cache()
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 93, in _populate_cache
    self._result_cache.append(self.next())
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1137, in next
    raw_doc = self._cursor.next()
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1182, in _cursor
    self._cursor_obj = self._collection.find(self._query,
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1215, in _query
    self._mongo_query = self._query_obj.to_query(self._document)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 92, in to_query
    query = query.accept(QueryCompilerVisitor(document))
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 157, in accept
    return visitor.visit_query(self)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 80, in visit_query
    return transform.query(self.document, **query.query)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/queryset/transform.py", line 87, in query
    value = field.prepare_query_value(op, value)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/fields.py", line 943, in prepare_query_value
    return self.to_mongo(value)
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongoengine/fields.py", line 912, in to_mongo
    id_field_name = self.document_type._meta['id_field']
  File "/home/cis/ENV/photoapp/local/lib/python2.7/site-packages/mongodbforms/documentoptions.py", line 87, in __getitem__
    return self._wrapped.__getitem__(key)
AttributeError: 'object' object has no attribute '__getitem__'

这是我的模特:

PERMISSION_CHOICES = (
    ('friend', 'Friend'),
    ('public', 'public'),
    ('private', 'Private'),
)


class PhotoAlbum(Document):
    name = StringField(max_length=200)
    slug = StringField(required=True, max_length=250, unique=True)
    created_at = DateTimeField(default=datetime.now)
    user = ReferenceField(User)
    is_role = StringField(max_length=50,required=True, choices = PERMISSION_CHOICES)

观点是:

@login_required
def album_view(request):
    """
    This method for show album and photo listing galary of login user
    """
    try:
        profile_pic = Photo.objects.get(user=request.user, profile_pic=True)
        profile_album = profile_pic.album
    except:
        profile_album = False

    albums = PhotoAlbum.objects.filter(user=request.user)
    #albums = PhotoAlbum.objects(user=request.user)
    #albums = PhotoAlbum.objects()
    #album = albums.filter(user=request.user)

    ctx = {
        'albums': albums,
        'profile_album': profile_album
    }

    return render(request, 'photologue/album_view.html', ctx)

我做了很多关于此的研究,我认为这个问题会通过mongoengine进行缓存查询,但我在查询时也尝试no_cache,但我仍然遇到同样的错误。

1 个答案:

答案 0 :(得分:0)

Twig无法迭代你的专辑。你需要添加" .all()"到#34;专辑= PhotoAlbum.objects.filter(user = request.user)"的结尾。或者更好,在上下文中进行。

ctx = {
    'albums': albums.all(),
    'profile_album': profile_album
}