Python Django-将两个模型序列化程序组合成一个JSON响应

Python Django-将两个模型序列化程序组合成一个JSON响应,python,django,django-rest-framework,Python,Django,Django Rest Framework,我有两种型号的表和卡。我正在尝试将这两种方法结合起来,生成一个JSON响应 [ { "id": 1, "name": "List of things to do" }, { "id": 2, "name": "one more" } ] [ { "id": 1, "title": "My first scrum card", "descrip

我有两种型号的表和卡。我正在尝试将这两种方法结合起来,生成一个JSON响应

[
    {
        "id": 1,
        "name": "List of things to do"
    },
    {
        "id": 2,
        "name": "one more"
    }
] 
[
    {
        "id": 1,
        "title": "My first scrum card",
        "description": "list things todo here",
        "story_points": null,
        "business_value": null,
        "list": 1
    },
    {
        "id": 2,
        "title": "File my taxes",
        "description": "fill it before 1st of nov",
        "story_points": null,
        "business_value": null,
        "list": 1
    },
]
我的列表JSON响应

[
    {
        "id": 1,
        "name": "List of things to do"
    },
    {
        "id": 2,
        "name": "one more"
    }
] 
[
    {
        "id": 1,
        "title": "My first scrum card",
        "description": "list things todo here",
        "story_points": null,
        "business_value": null,
        "list": 1
    },
    {
        "id": 2,
        "title": "File my taxes",
        "description": "fill it before 1st of nov",
        "story_points": null,
        "business_value": null,
        "list": 1
    },
]
我的卡片JSON响应

[
    {
        "id": 1,
        "name": "List of things to do"
    },
    {
        "id": 2,
        "name": "one more"
    }
] 
[
    {
        "id": 1,
        "title": "My first scrum card",
        "description": "list things todo here",
        "story_points": null,
        "business_value": null,
        "list": 1
    },
    {
        "id": 2,
        "title": "File my taxes",
        "description": "fill it before 1st of nov",
        "story_points": null,
        "business_value": null,
        "list": 1
    },
]
我的
序列化程序.py
文件

from rest_framework import serializers
from .models import List, Card

class CardSerializer(serializers.ModelSerializer):

    class Meta:
        model = Card
        fields = '__all__'

class ListSerializer(serializers.ModelSerializer):
    cards = CardSerializer(read_only=True, many=True)

    class Meta:
        model = List
        fields ='__all__'
from rest_framework.viewsets import ModelViewSet
from .models import List, Card
from .serializers import ListSerializer, CardSerializer

class ListViewSet(ModelViewSet):
    queryset = List.objects.all()
    serializer_class = ListSerializer

class CardViewSet(ModelViewSet):
    queryset = Card.objects.all()
    serializer_class = CardSerializer
我的
api.py
文件

from rest_framework import serializers
from .models import List, Card

class CardSerializer(serializers.ModelSerializer):

    class Meta:
        model = Card
        fields = '__all__'

class ListSerializer(serializers.ModelSerializer):
    cards = CardSerializer(read_only=True, many=True)

    class Meta:
        model = List
        fields ='__all__'
from rest_framework.viewsets import ModelViewSet
from .models import List, Card
from .serializers import ListSerializer, CardSerializer

class ListViewSet(ModelViewSet):
    queryset = List.objects.all()
    serializer_class = ListSerializer

class CardViewSet(ModelViewSet):
    queryset = Card.objects.all()
    serializer_class = CardSerializer
My
models.py

from django.db import models

class  List(models.Model):
    name = models.CharField(max_length=50) 

    def __str__(self):
        return "List : {}".format(self.name)

class Card(models.Model): # to create card table
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True) 
    list = models.ForeignKey(List, related_name = "card" 
    ,on_delete=models.PROTECT) # creating a foriegn key for storing list
    story_points = models.IntegerField(null=True, blank = True)
    business_value = models.IntegerField(null=True, blank = True)
    def __str__(self):
        return "Card : {}".format(self.title)
如何在我的列表JSON响应中实现以下内容

[
    {
        "id": 1,
        "cards":[
        {
            "id": 1,
            "title": "My first scrum card",
            "description": "list things todo here",
            "story_points": null,
            "business_value": null,
            "list": 1
        }],
        "name": "List of things to do"
    },
    {
        "id": 2,
        "cards":[
        {
            "id": 2,
            "title": "File my taxes",
            "description": "fill it before 1st of nov",
            "story_points": null,
            "business_value": null,
            "list": 1
        }],
        "name": "one more"
    },
]
我曾尝试过这样做,但未能付诸实施。所以我把它贴在这里

标题
非常感谢。

将列表
相关的\u name
更改为
卡片
,并且
列表视图集
将满足您的需要

class Card(models.Model): # to create card table
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True) 
    list = models.ForeignKey(List, related_name = "cards"
    ,on_delete=models.PROTECT)
...

您还可以使用SerializerMethodeField


您可以发布您的模型吗?@slider正在添加模型