Why do I get an InvalidDocument exception when saving an object into MongoDB in Django for the first time?

梦想与她 提交于 2019-12-21 23:54:59

问题


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.


回答1:


I got this error until updating the django-mongodb-engine library to the current version in the 'develop' branch of the github repository. Simply using the instructions on the article shown will import the master branch.




回答2:


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.




回答3:


Does this work:

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

Another thing to try:

class Post(models.Model):
    title = models.CharField(primary_key=True, max_length=200)

I think by default Django uses auto-generated numeric values as primary keys. This would force your title to be your primary key... it's a char string, so it might solve the problem you're seeing. It's just a guess, because the actual error your seeing doesn't make too much sense to me.




回答4:


From what I see, your model looks just fine. The error is funny; it is saying that the key to the attribute you are calling as "title" cannot be anything other than a string, but if I'm not mistaken, the string here is the key "title" and the value is the CharField() field, together representing a key/value pair in the Post document in your MongoDB. Try isolating the problem by jumping into the django shell:

python manage.py shell

and building your Post object from scratch. Use the model that dragonx has mentioned. Simple is best to debug these problems. If you are getting the same error when you try either to call Post.objects.create(p) for a Post object p, or when you call p.save(), try re-writing the Post model and give it another shot.

Finally, you can show us your settings.py file and see what your mongodb settings are there. If you say you are using django-nonrel, then there are basic checks you can make to ensure that python, django, and mongodb are talking to each other.

Let us know how you're doing.



来源:https://stackoverflow.com/questions/10163737/why-do-i-get-an-invaliddocument-exception-when-saving-an-object-into-mongodb-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!