django restframework中的to_表示不适用于自定义序列化程序。SerializerMethodField
我对自定义结构和自定义字段有点困惑,附加了我的参考实现。如果你看到你的输出不符合预期,有人知道为什么吗django restframework中的to_表示不适用于自定义序列化程序。SerializerMethodField,django,django-rest-framework,Django,Django Rest Framework,我对自定义结构和自定义字段有点困惑,附加了我的参考实现。如果你看到你的输出不符合预期,有人知道为什么吗 class ImageSerializer(serializers.ModelSerializer): thumb = serializers.ImageField() class Meta: model = Image fields = ('thumb',) class ProductSerializer(serializers.ModelS
class ImageSerializer(serializers.ModelSerializer):
thumb = serializers.ImageField()
class Meta:
model = Image
fields = ('thumb',)
class ProductSerializer(serializers.ModelSerializer):
def product_url(self,obj):
request = self.context['request']
return request.build_absolute_uri(reverse('product', args=(obj.slug,)))
url = serializers.SerializerMethodField('product_url')
images = ImageSerializer(many=True)
class Meta:
model = Product
fields = ('url', 'images', 'id')
def to_representation(self,product):
raise Exception(product.url) # Not working
raise Exception(product.images) # Not working
raise Exception(product.id) # Working
这是错误消息
'Product' object has no attribute 'url'
注意:但如果我不使用to_表示覆盖,那么json响应就有url字段
我的解决方法
型号
class Product(models.Model):
title = models.CharField(max_length=256,null=True,blank=False)
class ProductImage(models.Model):
product = models.ForeignKey('Product',on_delete=models.CASCADE,null=True,blank=True,related_name='product_images')
image = models.ImageField(upload_to='product/',null=True)
thumb = ImageSpecField(source='image',
processors=[ResizeToFill(100, 1100)],
format='JPEG',
options={'quality': 70})
实际产出
{
"count":5,
"next":"http://localhost:8000/api/products/?format=json&page=2",
"previous":null,
"results":[
{
"id":1,
"images":[
{
"thumb":"http://localhost:8000/media/CACHE/images/product/Product1/fee2eb25a1d7b954632dd377aca39995.jpg"
},
{
"thumb":"http://localhost:8000/media/CACHE/images/product/Product2/a279c5057bb5ee6e06945f98d89cc411.jpg"
}
],
"url":"http://localhost:8000/product/wooden-furniture/"
}
]
}
{
"count":5,
"next":"http://localhost:8000/api/products/?format=json&page=2",
"previous":null,
"results":[
{
"id":1,
"images":[
"thumb":"http://localhost:8000/media/CACHE/images/product/Product1/fee2eb25a1d7b954632dd377aca39995.jpg",
"popup":"http://localhost:8000/media/CACHE/images/product/Product2/a279c5057bb5ee6e06945f98d89cc411.jpg" # Will work on it once I achieve it wil popup
],
"url":"http://localhost:8000/product/wooden-furniture/"
}
]
}
预期产出
{
"count":5,
"next":"http://localhost:8000/api/products/?format=json&page=2",
"previous":null,
"results":[
{
"id":1,
"images":[
{
"thumb":"http://localhost:8000/media/CACHE/images/product/Product1/fee2eb25a1d7b954632dd377aca39995.jpg"
},
{
"thumb":"http://localhost:8000/media/CACHE/images/product/Product2/a279c5057bb5ee6e06945f98d89cc411.jpg"
}
],
"url":"http://localhost:8000/product/wooden-furniture/"
}
]
}
{
"count":5,
"next":"http://localhost:8000/api/products/?format=json&page=2",
"previous":null,
"results":[
{
"id":1,
"images":[
"thumb":"http://localhost:8000/media/CACHE/images/product/Product1/fee2eb25a1d7b954632dd377aca39995.jpg",
"popup":"http://localhost:8000/media/CACHE/images/product/Product2/a279c5057bb5ee6e06945f98d89cc411.jpg" # Will work on it once I achieve it wil popup
],
"url":"http://localhost:8000/product/wooden-furniture/"
}
]
}
我的自定义结构试用版
def to_representation(self,obj):
return {
'id': obj.id,
'images': {
'thumb': obj.images # May be I have to work to get only first image here
},
'url': obj.url
}
当您自己重新发送数据时。所以你必须自己做其他事情 试试这个
class ProductSerializer(serializers.ModelSerializer):
def product_url(self,obj):
request = self.context['request']
return request.build_absolute_uri(reverse('product', args=(obj.slug,)))
class Meta:
model = Product
fields = ('url', 'images', 'id')
def to_representation(self, product):
image_objs = ProductImage.objects.filter(product=product)
images = ImageSerializer(image_objs, many=True)
return {
"id": product.id,
"images" images.data,
"url": self.product_url(product)
}
在您的案例中,
product
是模型product
的实例,据我所知,您的模型没有url
attr。@BearBrown是的,确实如此,但在我的案例中我如何实现呢。我有2个自定义字段,我需要用to_表示形成结构。。有什么想法吗?答案中的SerializerMethodField
看起来是个好主意,为什么要用to_表示法呢?因为我想要自己的结构,而不是使用默认的rest框架结构。。我的要求:(请展示你想要发布的模型和json