在Django中首次将对象保存到MongoDB中时,为什么会收到InvalidDocument异常? [英] Why do I get an InvalidDocument exception when saving an object into MongoDB in Django for the first time?

查看:197
本文介绍了在Django中首次将对象保存到MongoDB中时,为什么会收到InvalidDocument异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在噩梦中试图让MongoDB与Django一起工作。我现在已经成功安装了它,但是在首次尝试保存对象时会出错。我一直在追踪本教程,以及他们呈现的Post模型。 p>

以下是该模型的代码:

  from django.db import模型
从djangotoolbox.fields导入ListField

类Post(models.Model):
title = models.CharField(max_length = 200)
text = models.TextField ()
tags = ListField()
comments = ListField()

帖子实际上是在这里创建(并插入):

  post = Post.objects.create(
... title ='Hello MongoDB!',
... text ='只是想从Django中删除一个笔记,Cya!',
... tags = ['mongodb','django'],
...评论= ['评论1','评论2']
...)

完整的堆栈跟踪可以在这里找到。错误本身是:

  InvalidDocument:文档必须只有字符串键,键为< django.db.models.fields。 CharField对象在0x22cae50> 

MongoDB显然正在运行,因为它希望键是字符串而不是整数。但为什么朗姆酒去了? Err,为什么标准的Django对象不能保存到MongoDB数据库?



我添加了被忽略的所需的CharField参数max_length。在这种情况下它不起作用,如果我也删除列表也不行。

解决方案

我也遵循这个教程,并有同样的错误。我设法解决了这个问题。
我做错了我从github使用djangotoolbox( https://github.com/django- nonrel / djangotoolbox )存储库,这是版本0.9.2。然后我删除了它,并将其作为教程状态并使用bitbucket( https://bitbucket.org/wkornewald/djangotoolbox),这是djangotoolbox的0.9.1版本。现在运行正常。


I've been having a nightmare of a time trying to get MongoDB working with Django. I now have it successfully installed, but it errors upon the first attempt to save an object. I've been following this tutorial, and the Post model they present I have copied precisely.

Here is the code for the model:

from django.db import models
from djangotoolbox.fields import ListField

class Post(models.Model):
    title = models.CharField(max_length=200)
    text = models.TextField()
    tags = ListField()
    comments = ListField()

The post is actually created (and inserted) here:

post = Post.objects.create(
...     title='Hello MongoDB!',
...     text='Just wanted to drop a note from Django. Cya!',
...     tags=['mongodb', 'django'],
...     comments=['comment 1', 'comment 2']
... ) 

The full stack trace can be found here. The error itself is:

InvalidDocument: documents must have only string keys, key was <django.db.models.fields.CharField object at 0x22cae50>

Clearly MongoDB is functioning, because it wants the keys to be strings instead of integers. But why is the rum gone? Err, why are standard Django objects not able to save into the MongoDB database?

I have added the required CharField parameter max_length that was overlooked. It does not work in this case, nor does it if I also remove the lists.

解决方案

I have also followed this tutorial and had same error. I managed to fix this problem. What I did wrong is I used djangotoolbox from github (https://github.com/django-nonrel/djangotoolbox) repository, which was ver 0.9.2. Then I removed this and did it as tutorial states and used bitbucket (https://bitbucket.org/wkornewald/djangotoolbox) that is on ver 0.9.1 of djangotoolbox. Now it works fine.

这篇关于在Django中首次将对象保存到MongoDB中时,为什么会收到InvalidDocument异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆