djangorestframework序列化时一同返回外键关联表的数据信息

models.py

class Snippet(models.Model):
    created = models.DateTimeField(default=ytime)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
    style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
    group = models.ForeignKey('Group', related_name='snippet', null=True) #建立外键,一定要写上related_name

    class Meta:
        ordering = ('created',)


class Group(models.Model):
    class_room = models.CharField(max_length=50)
    number = models.IntegerField(max_length=10)
    class Meta:
        db_table = 'group'

serializers.py

class SnippetSerializers(serializers.ModelSerializer):
    class Meta:
        model = Snippet
        fields = '__all__'


class GroupSerializers(serializers.ModelSerializer):
    snippet = SnippetSerializers(read_only=True, many=True) # 外键的related_name,many=True不能缺少
    class Meta:
        model = Group
        fields = '__all__'

views.py

class SnippetView(viewsets.ModelViewSet):
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializers
    filter_class = SnippetFilter
    search_fields = ('title', '=style',)

class GroupView(viewsets.ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializers

这样在访问路由返回Group的数据时,就会包含字表中对应外键是Group的数据了
如下:

{
    "count": 3,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "snippet": [
                {
                    "id": 1,
                    "created": "2019-03-04T00:00:00",
                    "title": "python-test",
                    "code": "abc",
                    "linenos": true,
                    "language": "0",
                    "style": 0,
                    "group": 1
                },
                {
                    "id": 2,
                    "created": "2019-03-04T06:42:18.399214",
                    "title": "friendly-test",
                    "code": "hello",
                    "linenos": false,
                    "language": "python",
                    "style": "friendly",
                    "group": 1
                },
              
            ],
            "class_room": "one",
            "number": 10
        },
        {
            "id": 2,
            "snippet": [
                {
                    "id": 6,
                    "created": "2019-03-04T09:25:24.031538",
                    "title": "",
                    "code": "how are you",
                    "linenos": false,
                    "language": "python",
                    "style": 1,
                    "group": 2
                },
                {
                    "id": 7,
                    "created": "2019-03-04T09:27:58.587445",
                    "title": "",
                    "code": "how are you",
                    "linenos": false,
                    "language": "python",
                    "style": 1,
                    "group": 2
                }, 
            ],
            "class_room": "two",
            "number": 20
        },
        {
            "id": 3,
            "snippet": [
                {
                    "id": 10,
                    "created": "2019-03-04T09:54:08.189380",
                    "title": "",
                    "code": "how are you",
                    "linenos": false,
                    "language": "python",
                    "style": 1,
                    "group": 3
                },
                {
                    "id": 11,
                    "created": "2019-03-04T09:58:32.201079",
                    "title": "",
                    "code": "how are you",
                    "linenos": false,
                    "language": "python",
                    "style": 1,
                    "group": 3
                },
                {
                    "id": 12,
                    "created": "2019-03-04T18:06:41",
                    "title": "",
                    "code": "hello",
                    "linenos": false,
                    "language": "python",
                    "style": "friendly",
                    "group": 3
                }
            ],
            "class_room": "three",
            "number": 30
        }
    ]
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值