【问题标题】:How can i show image field inside my RentListAPIView?如何在我的 RentListAPIView 中显示图像字段?
【发布时间】:2016-08-26 19:32:53
【问题描述】:

我有两个模型出租和画廊。我的模型是这样的

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 设计是这样的

但我也需要图像字段,每个租金都有多个这样的图像

【问题讨论】:

  • 只是添加gallery = GalleryListSerializer() 不起作用吗?
  • 我试过了。它说 GalleryListSerializer 未定义。当我将 GalleryListSerializer 类从 RentListSerializer 的底部移动到顶部时,我在 /api/rentals/'RelatedManager' 对象没有属性'rental' 处得到 AttributeError 错误
  • 请看我的回答。并且您确实需要将其从底部移动到顶部。

标签: python django django-rest-framework django-1.9


【解决方案1】:

看看 django rest 框架中的Nested Relationships

如果该字段用于表示to-many 关系,则应在序列化器字段中添加many=True 标志。

你现在拥有的是这个,我认为它不起作用,这就是你评论它的原因。:

# gallery = GalleryListSerializer()

你需要的是:

gallery = GalleryListSerializer(many=True, read_only=True)

【讨论】:

  • 我不需要从下往上移动吗?
  • 是的,您确实需要这样做。
  • 好的,我会尝试并告诉你
  • 是的,它有效,但使用 many=True 如何解决 /api/rentals/ 'RelatedManager' 对象没有属性 'rental' 的 ttributeError 问题
  • 对不起,我没有看到你的更新答案。不要介意我之前的评论。感谢您的帮助。我会看的。
猜你喜欢
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
  • 2023-03-12
  • 1970-01-01
  • 2011-01-11
  • 2016-10-19
  • 1970-01-01
相关资源
最近更新 更多