Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Return JSON object using MongoEngine Pymongo with Django?

So I'm trying to return a JSON object for a project. I've spent a few hours trying to get Django just returning the JSON.

Heres the view that we've been working with:

def json(request, first_name):
    user = User.objects.all()
    #user = User.objects.all().values()
    result = simplejson.dumps(user, default=json_util.default)
    return HttpResponse(result)

Here's my model:

class User(Document):  
    gender = StringField( choices=['male', 'female', 'Unknown']) 
    age = IntField()
    email = EmailField()
    display_name = StringField(max_length=50)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)
    location = StringField(max_length=50)
    status = StringField(max_length=50)
    hideStatus = BooleanField()
    photos = ListField(EmbeddedDocumentField('Photo')) 
    profile =ListField(EmbeddedDocumentField('ProfileItem'))
    allProfile = ListField(EmbeddedDocumentField('ProfileItem')) #only return for your own profile

This is what it's returning:

[<User: User object>, <User: User object>] is not JSON serializable

Any thoughts on how I can just return the JSON?

like image 834
JohnAllen Avatar asked May 27 '11 02:05

JohnAllen


People also ask

What is the use of mongoengine in pymongo?

MongoEngine is an ORM (object relation mapping) layer on the top of PyMongo written in python. Using MongoEngine will grant you additional benefits like fields DictField and ListField which you will see very useful when dealing with unstructured JSON data in MongoDB. Add these lines in settings.py and remove or comment out DATABASES section

Is it possible to use Djongo with JSON data?

Djongo is great and allows us to use Django ORM and most of Django features as it is, But when you are dealing with JSON data you will need your models to have DictField and ListField which is present in MongoEngine but not is Djongo. For example below data is complex and can be handled in MongoEngine easily but not in Djongo.

How to write JSON data to MongoDB in Python?

PyMongo is very efficient for writing JSON data to MongoDB and allows the use of MongoDB queries in the Python code itself. We can retrieve data in a dictionary like syntax using PyMongo.

How to use MongoDB with Django?

PyMongo: PyMongo is the standard driver through which MongoDB can interact with Django. It is the official and preferred way of using MongoDB with Python. PyMongo provides functionality to perform all the database actions like search, delete, update, and insert. Since PyMongo is available with PyPI, you can quickly install it using a pip command.


2 Answers

With MongoEngine 0.8 or greater, objects and querysets have a to_json() method.

>>> User.objects.to_json()
like image 188
Teisman Avatar answered Oct 20 '22 00:10

Teisman


simplejson.dumps() doesn't know how to "reach into" your custom objects; the default function, json_util.default must just be calling str() or repr() on your documents. (Is json_util custom code you've written? If so, showing its source here could prove my claim.)

Ultimately, your default function will need to be able to make sense of the MongoEngine documents. I can think of at least two ways that this might be implemented:

  1. Write a custom default function that works for all MongoEngine documents by introspecting their _fields attribute (though note that the leading underscore means that this is part of the private API/implementation detail of MongoEngine and may be subject to change in future versions)

  2. Have each of your documents implement a as_dict method which returns a dictionary representation of the object. This would work similarly to the to_mongo method provided on documents by MongoEngine, but shouldn't return the _types or _cls fields (again, these are implementation details of MongoEngine).

I'd suggest you go with option #2: the code will be cleaner and easier to read, better encapsulated, and won't require using any private APIs.

like image 42
dcrosta Avatar answered Oct 19 '22 23:10

dcrosta