Django休息框架嵌套自引用对象 [英] Django rest framework nested self-referential objects

查看:193
本文介绍了Django休息框架嵌套自引用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型看起来像这样:

I have model that looks like this:

class Category(models.Model):
    parentCategory = models.ForeignKey('self', blank=True, null=True, related_name='subcategories')
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=500)

我设法使用序列化程序获取所有类别的平面json表示:

I managed to get flat json representation of all categories with serializer:

class CategorySerializer(serializers.HyperlinkedModelSerializer):
    parentCategory = serializers.PrimaryKeyRelatedField()
    subcategories = serializers.ManyRelatedField()

    class Meta:
        model = Category
        fields = ('parentCategory', 'name', 'description', 'subcategories')

现在我想做的是子类列表具有子类别的内联json表示,而不是它们的ids。我如何用django-rest-framework来做到这一点?我试图在文档中找到它,但它似乎不完整。

Now what I want to do is for subcategories list to have inline json representation of subcategories instead of their ids. How would I do that with django-rest-framework? I tried to find it in documentation, but it seems incomplete.

推荐答案

而不是使用ManyRelatedField,使用嵌套的serializer作为您的字段:

Instead of using ManyRelatedField, use a nested serializer as your field:

class SubCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ('name', 'description')

class CategorySerializer(serializers.ModelSerializer):
    parentCategory = serializers.PrimaryKeyRelatedField()
    subcategories = serializers.SubCategorySerializer()

    class Meta:
        model = Category
        fields = ('parentCategory', 'name', 'description', 'subcategories')

如果你想处理任意的嵌套字段,你应该看看自定义默认字段部分文档。您目前无法直接将序列化器声明为本身的字段,但您可以使用这些方法来覆盖默认情况下使用的字段。

If you want to deal with arbitrarily nested fields you should take a look at the customising the default fields part of the docs. You can't currently directly declare a serializer as a field on itself, but you can use these methods to override what fields are used by default.

class CategorySerializer(serializers.ModelSerializer):
    parentCategory = serializers.PrimaryKeyRelatedField()

    class Meta:
        model = Category
        fields = ('parentCategory', 'name', 'description', 'subcategories')

        def get_related_field(self, model_field):
            # Handles initializing the `subcategories` field
            return CategorySerializer()






实际上,正如你注意到上述isn没关系
这是一个黑客,但你可能会尝试在序列化器已经声明后添加该字段。


Actually, as you've noted the above isn't quite right. This is a bit of a hack, but you might try adding the field in after the serializer is already declared.

class CategorySerializer(serializers.ModelSerializer):
    parentCategory = serializers.PrimaryKeyRelatedField()

    class Meta:
        model = Category
        fields = ('parentCategory', 'name', 'description', 'subcategories')

CategorySerializer.base_fields['subcategories'] = CategorySerializer()

声明递归关系的机制是需要添加的。

A mechanism of declaring recursive relationships is something that needs to be added.

编辑:请注意,现在有一个专门处理这种用例的第三方软件包。请参阅 djangorestframework-recursive

Edit: Note that there is now a third-party package available that specifically deals with this kind of use-case. See djangorestframework-recursive.

这篇关于Django休息框架嵌套自引用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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