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?
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
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.
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.
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.
With MongoEngine 0.8 or greater, objects and querysets have a to_json()
method.
>>> User.objects.to_json()
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:
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With