Paragon
Paragon

Reputation: 2722

Why does Python raise an exception when I save() a model instance into a MongoDB database?

I've been using this tutorial to experiment and start up my first Django app using MongoDB. In the tutorial, they build a simple Post class as follows:

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

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

Pretty simple. They then lead me through building a Post object with:

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']
... )

It immediately sends a rather long stack trace which I have copied here. The final error is:

TypeError: encoder expected a mapping type but got: {<django.db.models.fields.CharField object at 0x2fc5f10>: 'Hello MongoDB!', <djangotoolbox.fields.ListField object at 0x2fccd10>: ['comment 1', 'comment 2'], '_id': ObjectId('4f8a22f8db0ee4386f000000'), <djangotoolbox.fields.ListField object at 0x2fcc750>: ['django', 'mongodb'], <django.db.models.fields.TextField object at 0x2fcc290>: 'text test'}

It seems to be expecting completely different data types, and I have no idea where to go on this. Google searches have turned up nothing of use. Any tips or solutions would be a boon.

Thanks,

ParagonRG

Edit: I have realized that it actually errors when the initial object is created with Post.objects.create() when I correctly follow the tutorial. I therefore have the same problem, but without the save() function.

Edit: Typing pip freeze to display the current installed Python packages in my virtual environment gives me:

Django==1.3.1
django-mongodb-engine==0.4.0
djangotoolbox==0.9.2
pymongo==2.1.1
wsgiref==0.1.2

Upvotes: 2

Views: 2024

Answers (2)

Jonas H.
Jonas H.

Reputation: 2491

I just tried that with the exact setup described in the tutorial and couldn't reproduce this :-(

Are you sure you exactly followed the installation instructions? In particular, did you use the exact same repositories?

Which version of PyMongo are you using?

Upvotes: 1

Frantz Romain
Frantz Romain

Reputation: 846

save() is usually used when making changes to an model object or creating one.This url should get you going. https://docs.djangoproject.com/en/dev/topics/db/queries/

Upvotes: 0

Related Questions