如何在Django REST中序列化层次关系 [英] How to serialize hierarchical relationship in Django REST

查看:254
本文介绍了如何在Django REST中序列化层次关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用django-mptt进行层次化的Django模型,它看起来像:

 类UOMCategory(MPTTModel,BaseModel )

这表示不同测量单位的类别

name = models.CharField(max_length = 50,unique = True)
description = models.CharField(max_length = 50,unique = True)
parent = TreeForeignKey('self',null = True,blank = True,related_name ='%(app_label)s _%(class)s_sub_uom_categories'

现在的问题是我使用Django REST框架创建了一个REST API;如何确保父字段返回序列化数据?



以下是模型序列化程序:

 类UOMCategorySerializer(BaseModelSerializer):

用于UOMCategory模型的REST API序列化模型

class Meta:
model = UOMCategory


解决方案

在DRF中,可以使用串行器作为字段在另一个串行器。但是,递归是不可能的。



汤姆·克里斯蒂在另一个问题上发布了一个解决方案( Django休息框架嵌套的自引用对象)。他的解决方案也适用于您的问题。



在您的UOMCategorySerializer.Meta类中,您指定要使用的字段,还列出父和/或子字段) 那里。然后你使用汤姆克里斯蒂斯解决方案。



在你的情况下,这将给出:

  class UOMCategorySerializer(ModelSerializer):
class Meta:
model = UOMCategory
fields =('name','description','parent','children')

Tom Christies解决方案:通过指定要为父母和/或孩子使用的字段,您可以避免使用太多(可能的无止境)递归:

  UOMCategorySerializer.base_fields ['parent'] = UOMCategorySerializer()
UOMCategorySerializer.base_fields ['children '] = UOMCategorySerializer(many = True)

上述在类似的情况下适用于我。 p>

I have a Django model that is hierarchical using django-mptt, which looks like:

class UOMCategory(MPTTModel, BaseModel):
    """
        This represents categories of different unit of measurements.
    """
    name = models.CharField(max_length=50, unique=True)
    description = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='%(app_label)s_%(class)s_sub_uom_categories')

The problem now is I created a REST API using Django REST Framework; how do I make sure that parent field returns serialized data?

Here is the Model Serializer:

class UOMCategorySerializer(BaseModelSerializer):
    """
    REST API Serializer for UOMCategory model
    """
    class Meta:
        model = UOMCategory

解决方案

In DRF you can use a serializer as a field in another serializer. However, recursion is not possible.

Tom Christie posted a solution on another question (Django rest framework nested self-referential objects). His solution will also work with your problem.

In your UOMCategorySerializer.Meta class you specify the fields you want to use, also list the parent and/or children field(s) there. Then you use Tom Christies solution.

In your case this would give:

class UOMCategorySerializer(ModelSerializer):
    class Meta:
        model = UOMCategory
        fields = ('name', 'description', 'parent', 'children')

Tom Christies solution: By specifying what field to use for parent and/or children, you avoid using too much (and possibily endless) recursion:

UOMCategorySerializer.base_fields['parent'] = UOMCategorySerializer()
UOMCategorySerializer.base_fields['children'] = UOMCategorySerializer(many=True)

The above works for me in a similar situation.

这篇关于如何在Django REST中序列化层次关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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