klipstein/dojango — Issue #70: 70 - JSON serialization fails with ObjectId (Foreign keys) with MongoDB models 
Author: klipstein
Created: 2011-11-06 13:04:29+00:00
State: open

I am using django-nonrel with mongoDB.
In my application there is a model that (stripped down) looks as such:

from django.db.models import *
class MyModel(Model):
id = CharField(max_length=24, primary_key=True)
references = ListField()

Here, "references" contains references to other fields in my database, and therefore correspond to the type pymongo.objectid.ObjectId.

When I try to send instances of this models with the json_response decorator, it fails because ObjectId is not serializable. I made a small fix in util/__init__.py (diff below):

22a23,26
> from pymongo.objectid import ObjectId
> except:
>	ObjectId = None
> try:
140a145,146
> elif isinstance(data, ObjectId):
>	ret = str(data)

Would be swell if this was implemented :)

Original link: http://code.google.com/p/dojango/issues/detail?id=70