如何在我的 RentListAPIView 中显示图像字段?
How can i show image field inside my RentListAPIView?
我有两个模型出租和画廊。我的模型看起来像这样
Models.py(缩短字段较少)
class Rental(models.Model):
ownerName = models.CharField(_("Owner's Name"),max_length=255, blank=True,null=True,
help_text=_("Owner's Full Name"))
renter = models.ForeignKey(User,null=True,blank=True)
email = models.CharField(max_length=120,blank=True,null=True)
phoneNumber = models.PositiveIntegerField(blank=False,null=True,
help_text=_("Phone number of contact person"))
listingName = models.CharField(_("Lisitng Name"), max_length=255, blank=False,null=True,
help_text=_("Title of the rental space"))
class Gallery(models.Model):
rental = models.ForeignKey('Rental', null=True, on_delete=models.CASCADE,verbose_name=_('Rental'), related_name="gallery")
image = models.ImageField(blank=True,upload_to='upload/',null=True)
Serializers.py
class RentalListSerializer(ModelSerializer):
renter = SerializerMethodField()
# gallery = GalleryListSerializer()
class Meta:
model = Rental
def get_renter(self, obj):
return str(obj.renter.username)
class GalleryListSerializer(ModelSerializer):
class Meta:
model = Gallery
Views.py
class RentalListAPIView(ListAPIView):
# queryset = Rental.objects.all()
serializer_class = RentalListSerializer
filter_backends = [SearchFilter]
search_fields = ['place','city']
pagination_class = RentalPageNumberPagination
def get_queryset(self, *args, **kwargs):
# queryset_list = super(RentalListAPIView,self).get_queryset(*args, **kwargs)
queryset_list = Rental.objects.all()
query = self.request.GET.get('q') # this is a class based view so we need to use self
if query:
queryset_list = queryset_list.filter(
Q(place__icontains=query)|
Q(city__icontains=query)
).distinct()
return queryset_list
class GalleryListAPIView(ListAPIView):
# queryset = Rental.objects.all()
serializer_class = GalleryListSerializer
pagination_class = RentalPageNumberPagination
def get_queryset(self, *args, **kwargs):
queryset_list = Gallery.objects.all()
return queryset_list
我的 API 设计看起来像这样
但我也需要图像字段,每个租金都有多个这样的图像
看看 django rest 框架中的 Nested Relationships。
If the field is used to represent a to-many
relationship, you should add the many=True
flag to the serializer field.
你现在拥有的是这个,我认为它不起作用,这就是你评论它的原因。:
# gallery = GalleryListSerializer()
您需要的是:
gallery = GalleryListSerializer(many=True, read_only=True)
我有两个模型出租和画廊。我的模型看起来像这样
Models.py(缩短字段较少)
class Rental(models.Model):
ownerName = models.CharField(_("Owner's Name"),max_length=255, blank=True,null=True,
help_text=_("Owner's Full Name"))
renter = models.ForeignKey(User,null=True,blank=True)
email = models.CharField(max_length=120,blank=True,null=True)
phoneNumber = models.PositiveIntegerField(blank=False,null=True,
help_text=_("Phone number of contact person"))
listingName = models.CharField(_("Lisitng Name"), max_length=255, blank=False,null=True,
help_text=_("Title of the rental space"))
class Gallery(models.Model):
rental = models.ForeignKey('Rental', null=True, on_delete=models.CASCADE,verbose_name=_('Rental'), related_name="gallery")
image = models.ImageField(blank=True,upload_to='upload/',null=True)
Serializers.py
class RentalListSerializer(ModelSerializer):
renter = SerializerMethodField()
# gallery = GalleryListSerializer()
class Meta:
model = Rental
def get_renter(self, obj):
return str(obj.renter.username)
class GalleryListSerializer(ModelSerializer):
class Meta:
model = Gallery
Views.py
class RentalListAPIView(ListAPIView):
# queryset = Rental.objects.all()
serializer_class = RentalListSerializer
filter_backends = [SearchFilter]
search_fields = ['place','city']
pagination_class = RentalPageNumberPagination
def get_queryset(self, *args, **kwargs):
# queryset_list = super(RentalListAPIView,self).get_queryset(*args, **kwargs)
queryset_list = Rental.objects.all()
query = self.request.GET.get('q') # this is a class based view so we need to use self
if query:
queryset_list = queryset_list.filter(
Q(place__icontains=query)|
Q(city__icontains=query)
).distinct()
return queryset_list
class GalleryListAPIView(ListAPIView):
# queryset = Rental.objects.all()
serializer_class = GalleryListSerializer
pagination_class = RentalPageNumberPagination
def get_queryset(self, *args, **kwargs):
queryset_list = Gallery.objects.all()
return queryset_list
我的 API 设计看起来像这样
但我也需要图像字段,每个租金都有多个这样的图像
看看 django rest 框架中的 Nested Relationships。
If the field is used to represent a
to-many
relationship, you should add themany=True
flag to the serializer field.
你现在拥有的是这个,我认为它不起作用,这就是你评论它的原因。:
# gallery = GalleryListSerializer()
您需要的是:
gallery = GalleryListSerializer(many=True, read_only=True)