for p in db.collection.find({"test_set":"abc"}):
thejson = json.dumps({'results':posts})
return  HttpResponse(thejson, mimetype="application/javascript")

During my Django/Python code, I can not return a JSON from the mongo query due to "ObjectID". The mistake states that "ObjectID" isn't serializable.

What must i do? A hacky way is always to loop through:

for p in posts:
    p['_id'] = ""

The json module will not work because of such things as the ObjectID.

Fortunately PyMongo provides json_util which ...

... allow[s] for specialized encoding and decoding of BSON documents into Mongo Extended JSON's Strict mode. This allows you scribe / decode BSON documents to JSON even if they will use special BSON types.

More here:

It's pretty simple to write a custom serializer which copes using the ObjectIds. Django already includes the one that handles decimals and dates, to help you extend that:

from django.core.serializers.json import DjangoJSONEncoder

class MongoAwareEncoder(DjangoJSONEncoder):
    """JSON encoder class that adds support for Mongo objectids."""
    def default(self, o):
        if isinstance(o, objectid.ObjectId):
            return str(o)
            return super(MongoAwareEncoder, self).default(o)

You can now just tell json to apply your custom serializer:

thejson = json.dumps({'results':posts}, cls=MongoAwareEncoder)