如何在 Django 中序列化多个对象

how to Serialize multiple objects in Django

我有 2 个模型用于管理员和成员职位,我想在一个 API 调用中获取这两个模型以在我的前端获取数据。我怎样才能做到这一点?

class ClinetAdminPosition(models.Model):
    name = models.CharField(max_length=128, null=True)
    company = models.ForeignKey(
        to="Company", on_delete=models.CASCADE, related_name="admin_positions", null=True
    )
    modified_at = models.DateTimeField(verbose_name="Updated", auto_now=True, editable=True)
    created_at = models.DateTimeField(verbose_name="Created", auto_now_add=True, editable=False)

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

class ClinetMangerPosition(models.Model):
    name = models.CharField(max_length=128, null=True)
    company = models.ForeignKey(
        to="Company", on_delete=models.CASCADE, related_name="manger_positions", null=True
    )
    modified_at = models.DateTimeField(verbose_name="Updated", auto_now=True, editable=True)
    created_at = models.DateTimeField(verbose_name="Created", auto_now_add=True, editable=False)

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

我想从 1 个 API 请求中获取两个模型的数据

变成这样:

[
  {
    "admin_positions": [
      {
        "name": "test",
        "company": 1
      },
      {
        "name": "test2",
        "company": 1
      },
      {
        "name": "test3",
        "company": 1
      }
    ],
    "manger_position": [
      {
        "name": "test",
        "company": 1
      },
      {
        "name": "test2",
        "company": 1
      },
      {
        "name": "test3",
        "company": 1
      }
    ]
  }
]

如有任何疑问,请告诉我

class ClinetMangerPositionSerializer(ModelSerializer)
      
      clas Meta:
         model = ClinetMangerPosition

         fields = ['name','company']

class ClinetAdminPositionSerializer(ModelSerializer):
      
      class Meta:
          model = ClinetAdminPosition
          fields = ['name','company']
class CompanySerializer(MOdelSerializer): 
      manger_position = ClinetAdminPositionSerializer(many=True,source 
         ='clinetmangerposition')
      admin_position = ClinetAdminPositionSerializer(many=True,source 
                  ='clinetadminposition')
      class Meta:
         model= Company
         fields = ['manger_position','admin_position']
      

你可以这样做。

**这是你的序列化器classes **

 class ClinetMangerPositionSerializer(ModelSerializer)
      
      clas Meta:
         model = ClinetMangerPosition

         fields = ['name','company']

class ClinetAdminPositionSerializer(ModelSerializer):
      
      class Meta:
          model = ClinetAdminPosition
          fields = ['name','company']

这将是您的观点class来显示数据。

from rest_framework.views import APIView
from . import serializers
from rest_framework.response import Response

class ViewName(APIView):
   
   def get(self, request):
      admin = ClinetAdminPosition.objects.all()
      manager = ClinetMangerPosition.objects.all()
      
      res :{
          "admin_positions": serializers.ClinetAdminPositionSerializer(admin, many=True).data
          "manager_positions":serializers.ClinetMangerPositionSerializer(manager, many-True).data
      }

      return Response(res)

像这样,