0

here is my models.py :

models.py

class Resource(Document):
    instruction = fields.StringField(required=True)
    picture = fields.ImageField(required=False)

and my serializer :

class ResourceSerializer(serializers.DocumentSerializer):
    class Meta:
        model = Resource
        fields = '__all__'

and my views.py

class ResourceViewSet(viewsets.ModelViewSet):
    lookup_field = 'id'
    serializer_class = ResourceSerializer

    def get_queryset(self):
        return Resource.objects()

and this is the response that i get when im trying to list the pictures

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
       "id": "5ac4886a91a48946801813c9",
       "instruction": "test",
       "picture": "5ac4886991a48946801813bf"
    }
]

note : mongodb using gridfs to storing the imagefiles

in case that mongodb using gridfs to storing the imagefiles , how can i send the images to client ?

keyvan
  • 1
  • 1

1 Answers1

0

I guess you want to send back the image url? If my assumption is correct you can try with SerializerMethodField.

It should look like this:

class ResourceSerializer(serializers.DocumentSerializer):
    picture = serializers.SerializerMethodField()
    class Meta:
        model = Resource
        fields = '__all__'

    def get_picture(self, obj):
        return obj.picture.url
T.Tokic
  • 1,264
  • 9
  • 10