DRF如何仅将第一个序列化对象返回到视图



在DRF中,有没有任何方法可以让单个嵌套图像显示在视图中。在我的示例中,您可以看到列出了2张照片。检索并显示第一张照片的最佳解决方案是什么?这个视图将用于房屋列表,我不希望显示每一张照片,因为可能会有一些用户上传30多张照片。

"url": "http://127.0.0.1:8000/api/v1/listings/1/",
"address": "8753 sherwood dr apt 111",
"image_set": [
{
"photo": "http://127.0.0.1:8000/media/listing_images/front-view.png"
},
{
"photo": "http://127.0.0.1:8000/media/listing_images/image34453.png
}
]
},

class ImageSerializerForListingDetail(serializers.ModelSerializer):
photo = serializers.ImageField(use_url=True, allow_empty_file=True)
class Meta:
model = Image
fields = ('photo', )
def get_image_url(self, listing):
return listing.photo.url

class ListingListSerializer(serializers.HyperlinkedModelSerializer):
photo = ImageSerializerForListingDetail(many=True, required=False)
class Meta:
model = Listing
fields = ('url', 'address', 'photo', )

class ListingViewSet(viewsets.ModelViewSet):
queryset = Listing.objects.all().order_by('id')
permission_classes = [IsOwnerOrReadOnly, ]
serializer_classes = {
'list': ListingListSerializer,
'retrieve': ListingSerializer
}
default_serializer_class = ListingSerializer
def get_serializer_class(self):
return self.serializer_classes.get(self.action, self.default_serializer_class)
def create(self, request, *args, **kwargs):
serializer = ListingSerializer(data=request.data, files=request.FILES)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

我刚刚尝试的当前代码,返回一个空的照片列表#

class ImageSerializerForListingDetail(serializers.ModelSerializer):
photo = serializers.ImageField(allow_empty_file=True)
class Meta:
model = Image
fields = ('photo', )
def get_image_url(self, listing):
return listing.photo.url

class ListingListSerializer(serializers.HyperlinkedModelSerializer):
first_image = ImageSerializerForListingDetail(many=True, required=False)
class Meta:
model = Listing
fields = ('url', 'address', 'first_image', )
def get_first_image(self, obj):
first_image = Image.objects.filter(listing=obj).first()
first_image_serializer = ImageSerializerForListingDetail(first_image)
return first_image_serializer.data

因为我不太确定你的模特怎么样,你想展示的第一张照片是哪张(例如最新上传的、第一次上传的、最新的id(。我将用一个通用的例子来解释解决方案。

请尝试serializers.SerializerMethodField()自定义要显示最新字段的字段。

假设用户模型中有一个组列表,并且您希望在用户的详细信息中显示最新的组id。

这里有一个例子:

class UserSerializer(serializers.ModelSerializer):
latest_group_id = serializers.SerializerMethodField()
def get_latest_group_id(self, obj):
return Group.objects.filter(user=obj).latest('id').id

由于您希望显示照片url而不是id,因此这里是伪代码。

class UserProfileSerializer(serializers.ModelSerializer):
latest_image = serializers.SerializerMethodField()
def get_latest_image(self, obj):
latest_image = Image.objects.filter(user=obj).latest('id')
latest_image_serializer = ImageSerializerForListingDetail(latest_image)
# Deal with edge cases, e.g. latest_image_serializer is not valid
return latest_image_serializer.data

您只能将第一项传递给序列化程序:

def get_queryset(self):
return  Listing.objects.all().order_by('id').first() if self.action == 'retrieve' else Listing.objects.all().order_by('id')

相关内容

  • 没有找到相关文章

最新更新