Why do I get an InvalidDocument exception when sav

2020-07-30 02:44发布

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.

4条回答
对你真心纯属浪费
2楼-- · 2020-07-30 03:06

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.

查看更多
迷人小祖宗
3楼-- · 2020-07-30 03:11

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.

查看更多
淡お忘
4楼-- · 2020-07-30 03:18

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.

查看更多
可以哭但决不认输i
5楼-- · 2020-07-30 03:29

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.

查看更多
登录 后发表回答