django rest-farmework嵌套关系 [英] django rest-farmework nested relationships

查看:48
本文介绍了django rest-farmework嵌套关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用django rest-farmework实现API,此处的嵌套关系存在问题。与外键关联的内容无法显示,具体代码如下:

Using django rest-farmework to implement the API, there is a problem in the nested relationship here. The content associated with the foreign key can not be displayed, the specific code is as follows:

models.py

models.py

class Category(models.Model):
    name = models.CharField(max_length=30)
    amount = models.IntegerField()

class Source(models.Model):
    name = models.CharField(max_length=50)
    rss_link = models.URLField()
    amount = models.IntegerField()
    # ForeignKey
    category = models.ForeignKey(Category)

views.py

class CategoryListView(APIView):
    def get(self, request):
        category = Category.objects.all()
        serializers = CategorySerializers(category, many=True)
        return Response(serializers.data)

serializers.py

serializers.py

class SourceSerializers(serializers.ModelSerializer):
    class Meta:
        model = Source
        fields = ("id","name","amount")

class CategorySerializers(serializers.ModelSerializer):
    source = SourceSerializers(many=True, read_only=True)

    class Meta:
        model = Category
        fields = ("id","name","amount","source")

程序运行结果:

[
    {
        "id": 1,
        "name": "默认分类",
        "amount": 0
    },
    {
        "id": 2,
        "name": "科技频道",
        "amount": 0
    }
]

为什么不能在结果中显示'source'?

Why can not show 'source' in the result?

我希望结果是这样的

I hope the result is like this

[
    {
        "id": 1,
        "name": "默认分类",
        "amount": "0",
        "source": [
            {
                "id": 34,
                "name": "博客园",
                "amount": "231"
            },
            {
                "id": 35,
                "name": "CSDN",
                "amount": "643"
            }
        ]
    },
    {
        "id": 2,
        "name": "科技频道",
        "amount": "0",
        "source": []
    }
]


推荐答案

感谢Klaus D的评论,问题已经解决了。
我们可以在models.py中添加 related_name ='source',例如:

Thanks to Klaus D's comments, the problem is solved. We can add related_name = 'source' in the models.py like this:

class Source(models.Model):
    name = models.CharField(max_length=50)
    rss_link = models.URLField()
    amount = models.IntegerField()
    # ForeignKey
    category = models.ForeignKey(Category,related_name = 'source')

如果不在外键中添加related_name,则默认值为 source_set。

If you do not add related_name in the foreignkey, the default is "source_set".

因此,我们还可以解决以下问题:

So, we can also solve the problem like this:

#serializers.py
class CategorySerializers(serializers.ModelSerializer):
    source_set = SourceSerializers(many=True, read_only=True)

    class Meta:
        model = Category
        fields = ("id","name","amount","source_set")

这篇关于django rest-farmework嵌套关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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