【问题标题】:Formatting JSON in Serializer - Django REST在序列化器中格式化 JSON - Django REST
【发布时间】:2018-10-27 04:57:04
【问题描述】:

我是 Django 和 Django-REST 的新手,我的任务是创建一个序列化程序。 我的序列化程序的输出如下所示:

    {
"count": 1,
"next": null,
"previous": null,
"results": [
    {
        "id": 1,
        "name": "a",
        "description": "a",
        "price": "1.00",
        "images": [
            {
                "image_addr": "http://127.0.0.1:8000/media/products/Screenshot_from_2018-05-16_15-07-34.png",
                "product": 1
            },
            {
                "image_addr": "http://127.0.0.1:8000/media/products/Screenshot_from_2018-05-16_16-42-55.png",
                "product": 1
            }
        ]
    }
]
}

如何调整我的序列化程序,使我的输出看起来像这样:

   {
"count": 1,
"next": null,
"previous": null,
"results": [
    {
        "id": 1,
        "name": "a",
        "description": "a",
        "price": "1.00",
        "images": [
           "http://127.0.0.1:8000/media/products/Screenshot_from_2018-05-16_15-07-34.png",   
           "http://127.0.0.1:8000/media/products/Screenshot_from_2018-05-16_16-42-55.png"
        ]
    }
]
}

我使用的序列化程序是:

    class ProductImageSerializer(serializers.ModelSerializer):
        class Meta:
            model = ProductImage
            fields = ('image_addr', 'product')

    class ProductSerializer(serializers.ModelSerializer):
        images = ProductImageSerializer(many=True, read_only=True)
        class Meta:
            model = Product
            fields = ('id', 'name', 'description', 'price','images')

我使用的模型是:

    class ProductImage(models.Model):
        image_addr = models.FileField(upload_to='products/',null=True)
        product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')

    class Product(models.Model):
        name = models.CharField(max_length=150)
        price = models.DecimalField(max_digits=9, decimal_places=2)
        product_count = models.IntegerField(blank=True, default=0, validators=[MinValueValidator(0)])
        description = models.TextField()
        category = models.ManyToManyField(Category)
        objects = ProductManager()

我使用的观点是:

    class CategoryProductList(generics.ListAPIView):
        serializer_class = ProductSerializer

        def get_queryset(self):
            queryset = Product.objects.filter(category__id=self.kwargs['pk'])
            return queryset

【问题讨论】:

  • 您可以添加您的ProductProductImage 模型吗?

标签: json django django-rest-framework django-serializer


【解决方案1】:

您可以将SerializerMethodField 用于您的目的。

如下更改您的ProductSerializer

class ProductSerializer(serializers.ModelSerializer):
    images = serializers.SerializerMethodField(read_only=True, source='images')

    def get_images(self, model):
        return model.images.values_list('image_addr', flat=True)

    class Meta:
        model = Product
        fields = ('id', 'name', 'description', 'price', 'images')

【讨论】:

  • 很高兴为您提供帮助:)
猜你喜欢
  • 1970-01-01
  • 2013-12-05
  • 2015-10-29
  • 2016-03-11
  • 2012-01-23
  • 2015-03-06
  • 2015-11-02
  • 2014-07-24
  • 2013-03-05
相关资源
最近更新 更多