【发布时间】:2018-11-30 19:30:01
【问题描述】:
我有模特 - 商品、服装和儿童模特,如配饰、外套等:
class Commodity(models.Model):
atribute = models.CharField()
class Clother(models.Model):
commodity = models.ForeignKey(Commodity)
clother_atribute = models.CharField()
class Acessories(models.Model):
clother = models.ForeignKey(Clother)
acessories_atribute = models.CharField()
class Outwear(models.Model):
clother = models.ForeignKey(Clother)
outwear_atribute = models.CharField()
如何序列化父模型Commodity来调用所有垂直依赖?我想查询 Commodity id 并获取所有 Clother 属性和 Acessories 或 Outwear 属性。
【问题讨论】:
-
你使用 Django 序列化器吗?
-
我使用 Django REST Framework 模型序列化器。
-
好吧,既然 Django 中没有像“垂直依赖”这样的概念,你可能必须自己列出这些。是的,您可以浏览模型关系图,但检测循环会使事情变得复杂。
-
谢谢,注意了。希望能遇到这样的事情。
标签: django django-models django-rest-framework