在Django REST Framework中处理不同级别的嵌套 [英] Handling different levels of nesting in Django REST Framework

查看:69
本文介绍了在Django REST Framework中处理不同级别的嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果您使用模型:

For example, if you take the models:

class Region(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)

class Company(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    region = models.ForeignKey('Region', db_index=True)

class Staff(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    company = models.ForeignKey('Company', db_index=True)

我知道我不需要在这些模型中包含id,但我这样做是为了使其更清晰。

I know I don't need to include id in these models, but I've done so to make it clearer.

在此示例中,有时您只想返回一个区域列表。有时候,您想返回一个地区列表,以及每个地区下每个地区的公司列表。

In this example, sometimes you would want to simply return a list of Regions. Other times you'd want to return a list of Regions with a list of each Region's Companies under each Region.

我想,您还想要更多的详细信息您有一个地区,其公司子公司以及每个公司的职员子公司的列表。

You would also, I imagine, want even more detail where you have a list of Regions, their Company children, and each Company's Staff children.

什么是处理这些不同深度/细节级别的最佳方法?有关框架的意见。人们通常如何处理这个问题?

What's the best way to handle these different levels of depth/detail as far as the rest framework views are concerned. How do people normally deal with this?

我的意思是,当您说三个视图在顶层返回相同的内容时,您将使用哪种命名约定? ,唯一的区别是它们包括多少层嵌套?

By this I mean, what kind of naming conventions would you use when you have say three views returning the same thing at the top level, with the only difference being how many levels of nesting they include?

推荐答案

仅返回列表区域

->在调用时使用带有 many = True 的简单序列化器(这将返回字典列表)

-->Use simple serializer with many=True while calling it(This will return list of dictionaries)

class SimpleRegionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Region
        fields = ('id', 'name')

获取区域列表以及每个区域下每个区域的公司列表

For getting list of Regions with a list of each Region's Companies under each Region

->使用嵌套序列化器

 class CompanySerializer(serializers.ModelSerializer):
     class Meta:
         model = Company
         fields = ('id', 'name')

 class NestedRegionSerializer(serializers.ModelSerializer):
     company = CompanySerializer(many=True, read_only=True)

     class Meta:
         model = Region
         fields = ('id', 'name')

也将公司模式更改为

 class Company(models.Model):
     id = models.AutoField(primary_key=True)
     name = models.CharField(max_length=256)
     region = models.ForeignKey('Region', db_index=True, related_name='company')

related_name与NestedRegionSerializer中的序列化器字段相同。

related_name is the same as serializer field in NestedRegionSerializer.

别忘了运行迁移或同步数据库

Don't forget to run migrations or syncdb

有关更多信息,请参见 http://www.django- rest-framework.org/api-guide/relations/#nested-relationships 以获取更多参考。

For more information see http://www.django-rest-framework.org/api-guide/relations/#nested-relationships for more reference.

您可以使用任何级别的嵌套,如果你想n还将深度指定为元选项,它将提供有关嵌套深度的信息。

这篇关于在Django REST Framework中处理不同级别的嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆