【发布时间】:2017-09-08 13:35:40
【问题描述】:
我有一个models.py 有:
class Other(models.Model):
name = models.CharField(max_length=200)
class ModelA(models.Model):
name = models.CharField(max_length=200)
other = models.ForeignKey(Other, on_delete=models.PROTECT)
在我的其余 API 中,我想以 JsonResponse 的形式检索一个像这样的 json:
{
"modelA": {
"id": "modelA id automatically assigned by django model",
"name": "my modelA name",
"other": {
"id": "other thing id also automatically assigned by django model",
"name": "other thing name"
}
}
}
最“pythonic”的方法是什么?
【问题讨论】:
-
请出示您的序列化器
-
你在使用 Django Rest 框架吗?如果没有,你应该是。
-
是的,我正在使用它
-
我的序列化器是什么意思?这只是一个概念性的例子
标签: python django serialization django-models django-rest-framework