python Django 序列化为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/414543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django serialize to JSON
提问by Steven H.
I have a Django model (schedule) with the class of entity, that is the parent of Activity
, that is the parent of Event
.
我有一个带有实体类的 Django 模型(计划),即 的父类Activity
,即Event
.
class Entity(models.Model):
<...>
class Activity(models.Model):
<...>
team_entity = models.ForeignKey(Entity)
<...>
class Event(models.Model):
<...>
activity = models.ForeignKey(Activity)
<...>
How do I serialize and get both the child object and grand children as part of the JSON file?
如何序列化并获取子对象和孙子对象作为 JSON 文件的一部分?
采纳答案by Steven H.
I now use django-piston. This does the trick.
我现在使用 django-piston。这就是诀窍。
回答by Sergey Golovchenko
Before you do serialization, when retrieving your objects, to preserve the relationships use select_related() to get children, grandchildren, etc
在进行序列化之前,检索对象时,要保留关系,请使用 select_related() 获取子项、孙项等
see http://docs.djangoproject.com/en/dev/ref/models/querysets/
回答by gsiegman
It seems to me that the question the poster was asking was to end up with a result like:
在我看来,海报提出的问题最终会得到如下结果:
For instance, starting with these models:
例如,从这些模型开始:
class Entity(models.Model):
name = models.CharField(...)
class Activity(models.Model):
name = models.CharField(...)
team_entity = models.ForeignKey(Entity)
class Event(models.Model):
name = models.CharField(...)
activity = models.ForeignKey(Activity)
Result in JSON:
结果为 JSON:
{
"model": "Entity",
"name": "Dallas Cowboys",
"activities": [
{
"model": "Activity",
"name": "Practice"
},
{
"model": "Activity",
"name": "Game"
"events": [
{
"model": "Event",
"name": "vs Washington Redskins"
},
{
"model": "Event",
"name": "vs Green Bay Packers"
}
]
}
]
}
Thus keeping the parent-child-grandchild (not inheritence, but one-to-many relationship traversal). If this wasn't the initial poster's intention, I apologize...but if so I would like the answer to this as well.
从而保持父子孙子(不是继承,而是一对多的关系遍历)。如果这不是最初发布者的意图,我很抱歉……但如果是这样,我也希望得到这个答案。
回答by Bartosz Ptaszynski
I believe you can find your answer here: http://code.djangoproject.com/ticket/4656
我相信您可以在这里找到答案:http: //code.djangoproject.com/ticket/4656
This will become part of django serializers at some stage. Right now it should be able to just replace standard django serializers with this and work away.
这将在某个阶段成为 django 序列化程序的一部分。现在它应该能够用这个替换标准的 django 序列化程序并开始工作。
回答by Sarat
Have a look at serializing inherited models and objects from the Django documentation available at http://docs.djangoproject.com/en/dev/topics/serialization/?from=olddocs#inherited-models
从http://docs.djangoproject.com/en/dev/topics/serialization/?from=olddocs#inherited-models 中提供的 Django 文档中查看序列化继承的模型和对象
That should solve your problem.
那应该可以解决您的问题。
回答by Mayank Saxena
you can do this in simple two lines of code :
你可以用简单的两行代码来做到这一点:
from django.core import serializers
data = serializers.serialize("json", SomeModel.objects.all())