gpt4 book ai didi

django - DRF 如何只将第一个序列化对象返回到 View ?

转载 作者:行者123 更新时间:2023-12-05 03:54:59 25 4
gpt4 key购买 nike

在 DRF 中,有什么方法可以让单个嵌套图像显示在 View 中。在我的示例中,您可以看到列出了 2 张照片。仅检索和显示第一张照片的最佳解决方案是什么?此 View 将用于房屋列表,我不希望显示每张图片,因为可能会有一些用户上传 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

关于django - DRF 如何只将第一个序列化对象返回到 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60529390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com