Return image url in Django Rest Framework

后端 未结 4 2187
孤街浪徒
孤街浪徒 2020-12-31 11:05

I am using Django Rest Framework and have the following model:

class Picture(models.Model):
    some_field = models.ForeignKey(some_model)
    image = models         


        
4条回答
  •  Happy的楠姐
    2020-12-31 11:26

    Updating the image field in the serializer to use_url=True worked for me:

    class PictureSerialiser(serializers.ModelSerializer):
        image = serializers.ImageField(
                max_length=None, use_url=True
            )
        class Meta:
            model = Picture
            fields = ('field', 'image')
    

    I wasn't able to get the currently accepted answer (adding a custom get_image_url method to the serializer) to work in Django 2.2. I was getting error messages that I needed to update my model to include the field image_url. Even after updating the model it wasn't working.

提交回复
热议问题