Skip to content
Advertisement

how to filter images from a list of files in django-rest-framework

I have a Django rest server which server a list of files to react frontend. I would like to know if I can filter those files by images and display only images to my react frontend. I have searched a lot but cannot find anything useful. Please help me. Thank you in advance.

class FileListView(generics.ListAPIView):
    serializer_class = ListFileSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        owner = self.request.user.username
        return File_Uploader.objects.filter(owner=owner)

Advertisement

Answer

I finally got it working using this trick.

Added this field in my model.

is_image = models.BooleanField(default=False)

When a user uploads a file I am checking whether the file is image or not using the following code.

if 'image' in request.FILES["file"].content_type:
    request.data['is_image'] = True
else:
    request.data['is_image'] = False

Now I can easily filter files based upon this is_image field.

Please let me know if this is not a good solution..

Thank you for reviewing my question. I really appreciate it.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement