无法使用 MongoEngine Pymongo 和 Django 返回 JSON 对象?
所以我试图返回一个项目的 JSON 对象。我花了几个小时试图让 Django 返回 JSON。
这是我们一直在使用的视图:
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)
这是我的模型:
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
这就是它返回的内容:
[<User: User object>, <User: User object>] is not JSON serializable
关于如何返回 JSON 有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 MongoEngine 0.8 或更高版本中,对象和查询集具有
to_json()
方法。With MongoEngine 0.8 or greater, objects and querysets have a
to_json()
method.simplejson.dumps()
不知道如何“访问”您的自定义对象;default
函数json_util.default
必须只是在文档上调用str()
或repr()
。 (json_util
是您编写的自定义代码吗?如果是这样,在此处显示其源代码可以证明我的主张。)最终,您的
default
函数需要能够有意义MongoEngine 文档。我可以想到至少有两种方法可以实现这一点:编写一个自定义的
default
函数,通过内省其_fields
属性来适用于所有 MongoEngine 文档(尽管注意前导下划线意味着这是 MongoEngine 的私有 API/实现细节的一部分,并且在未来版本中可能会发生变化)让您的每个文档实现了一个
as_dict
方法,该方法返回对象的字典表示形式。这与 MongoEngine 在文档上提供的to_mongo
方法类似,但不应返回_types
或_cls
字段(同样,这些是MongoEngine 的实现细节)。我建议您使用选项#2:代码会更干净、更容易阅读、封装得更好,并且不需要使用任何私有 API。
simplejson.dumps()
doesn't know how to "reach into" your custom objects; thedefault
function,json_util.default
must just be callingstr()
orrepr()
on your documents. (Isjson_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 theto_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.
正如 dcrosta 建议你可以做这样的事情,希望对你有帮助。
文档定义
helper.py:
As dcrosta suggested you can do something like this, hope that will help you.
Document definition
helper.py: