如何使用 Django Rest Framework 在一个 APIVIEW 中获取多个 ModelSrializer

发布于 2025-01-11 07:14:32 字数 2660 浏览 0 评论 0 原文

我有 UserModel 每个用户有多个 package 并且每个包都有 priceprogram。 model.py:

class User(models.Model):
    name= models.CharField(max_length=100)
class Package(models.Model):
    package_name = models.CharField(max_length=100)
    user= models.ForeignKey(User, on_delete=models.CASCADE,related_name='user_package')
class Program(models.Model):
    title = models.CharField(max_length=100)
    user_package = models.ForeignKey(Package, on_delete=models.CASCADE)
class Price(models.Model):
    price = models.FloatField()
    user_package = models.ForeignKey(Package, on_delete=models.CASCADE)

序列化器看起来像:

class UserSerializer(NestedCreateMixin, NestedUpdateMixin,serializers.ModelSerializer):
    program = ProgramSerializer(source='user_program', many=True, read_only=True)
    package = PackageSerializer(source='user_package', many=True, read_only=True)
    price = PriceSerializer(source='price', many=True, read_only=True)
    class Meta:
        model = User
        fields= ("__all__")

views.py:

class user_apiView(APIView):
    def get(self, request):
        user= user.objects.all()
        serializer = UserSerializer(user, many = True)
        return Response(serializer.data)

我得到的是:

{
        "id": 1,
        "package": [
            {
                "id": 1,
                "package_name": "wfe",
                "user": 1
            },
            {
                "id": 2,
                "package_name": "wfe",
                "user": 1
            }
        ]
}

如何得到这个结果?

{
            "id": 1,
            "package": [
                {
                    "id": 1,
                    "package_name": "wfe",
                    "user": 1
                },
                {
                    "id": 2,
                    "package_name": "wfe",
                    "user": 1
                }
              ],
             "price": [
                {
                    "id": 1,
                    "price": "wfe",
                    "package": 1
                },
                {
                    "id": 2,
                    "price": "wfe",
                    "package": 2
                }
              ]
              "program": [
                {
                    "id": 1,
                    "title": "wfe",
                    "package": 1
                },
                {
                    "id": 2,
                    "title": "wfe",
                    "package": 2
                }
              ]
    }

I have UserModel each user has multiple package and each package have price and program.
model.py:

class User(models.Model):
    name= models.CharField(max_length=100)
class Package(models.Model):
    package_name = models.CharField(max_length=100)
    user= models.ForeignKey(User, on_delete=models.CASCADE,related_name='user_package')
class Program(models.Model):
    title = models.CharField(max_length=100)
    user_package = models.ForeignKey(Package, on_delete=models.CASCADE)
class Price(models.Model):
    price = models.FloatField()
    user_package = models.ForeignKey(Package, on_delete=models.CASCADE)

serializer look like:

class UserSerializer(NestedCreateMixin, NestedUpdateMixin,serializers.ModelSerializer):
    program = ProgramSerializer(source='user_program', many=True, read_only=True)
    package = PackageSerializer(source='user_package', many=True, read_only=True)
    price = PriceSerializer(source='price', many=True, read_only=True)
    class Meta:
        model = User
        fields= ("__all__")

views.py:

class user_apiView(APIView):
    def get(self, request):
        user= user.objects.all()
        serializer = UserSerializer(user, many = True)
        return Response(serializer.data)

and that what I get:

{
        "id": 1,
        "package": [
            {
                "id": 1,
                "package_name": "wfe",
                "user": 1
            },
            {
                "id": 2,
                "package_name": "wfe",
                "user": 1
            }
        ]
}

how can GET this RESULT?

{
            "id": 1,
            "package": [
                {
                    "id": 1,
                    "package_name": "wfe",
                    "user": 1
                },
                {
                    "id": 2,
                    "package_name": "wfe",
                    "user": 1
                }
              ],
             "price": [
                {
                    "id": 1,
                    "price": "wfe",
                    "package": 1
                },
                {
                    "id": 2,
                    "price": "wfe",
                    "package": 2
                }
              ]
              "program": [
                {
                    "id": 1,
                    "title": "wfe",
                    "package": 1
                },
                {
                    "id": 2,
                    "title": "wfe",
                    "package": 2
                }
              ]
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2025-01-18 07:14:32

问题是你的价格和程序模型与你的用户模型没有直接关系。如果你考虑一下这些关系,就像price ->;包-> user,所以你必须在包序列化器中获取这些关系,就像这样的

serializers.py

class ProgramSerializer(serializers.ModelSerializer):
    class Meta:
        model = Program
        fields= ("__all__")


class PriceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Price
        fields= ("__all__")

class PackageSerializer(serializers.ModelSerializer):
    program = ProgramSerializer(source='program_set', many=True, read_only=True)
    price = PriceSerializer(source='price_set', many=True, read_only=True)

    class Meta:
        model = Package
        fields= ("__all__")

class UserSerializer(serializers.ModelSerializer):
    package = PackageSerializer(source='user_package', many=True, read_only=True)
    
    class Meta:
        model = User
        fields= ("__all__")

请注意,但这不会以你提到的格式提供输出,价格和程序字段将被嵌套包下。

[
    {
        "id": 1,
        "package": [
            {
                "id": 1,
                "program": [
                    {
                        "id": 1,
                        "title": "program1",
                        "user_package": 1
                    }
                ],
                "price": [
                    {
                        "id": 1,
                        "price": 1000.0,
                        "user_package": 1
                    }
                ],
                "package_name": "package1",
                "user": 1
            },
            {
                "id": 2,
                "program": [
                    {
                        "id": 2,
                        "title": "program2",
                        "user_package": 2
                    }
                ],
                "price": [
                    {
                        "id": 2,
                        "price": 1500.0,
                        "user_package": 2
                    }
                ],
                "package_name": "package2",
                "user": 1
            }
        ],
        "name": "user1"
    }
]

如果您确实想要您发布的格式,您可能想看看 https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

The problem is that your price and program models are not directly related to your user model. If you consider the relations it is like price -> package -> user, so you will have to get those relations in the package serializer instead like this

serializers.py

class ProgramSerializer(serializers.ModelSerializer):
    class Meta:
        model = Program
        fields= ("__all__")


class PriceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Price
        fields= ("__all__")

class PackageSerializer(serializers.ModelSerializer):
    program = ProgramSerializer(source='program_set', many=True, read_only=True)
    price = PriceSerializer(source='price_set', many=True, read_only=True)

    class Meta:
        model = Package
        fields= ("__all__")

class UserSerializer(serializers.ModelSerializer):
    package = PackageSerializer(source='user_package', many=True, read_only=True)
    
    class Meta:
        model = User
        fields= ("__all__")

Note that this will however not give you the output in the format you mentioned though, the price and program fields will instead be nested under package.

[
    {
        "id": 1,
        "package": [
            {
                "id": 1,
                "program": [
                    {
                        "id": 1,
                        "title": "program1",
                        "user_package": 1
                    }
                ],
                "price": [
                    {
                        "id": 1,
                        "price": 1000.0,
                        "user_package": 1
                    }
                ],
                "package_name": "package1",
                "user": 1
            },
            {
                "id": 2,
                "program": [
                    {
                        "id": 2,
                        "title": "program2",
                        "user_package": 2
                    }
                ],
                "price": [
                    {
                        "id": 2,
                        "price": 1500.0,
                        "user_package": 2
                    }
                ],
                "package_name": "package2",
                "user": 1
            }
        ],
        "name": "user1"
    }
]

If you really want the format in the way you posted you might instead want to take a look at https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文