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.
JavaScript
x
8
1
class FileListView(generics.ListAPIView):
2
serializer_class = ListFileSerializer
3
permission_classes = (permissions.IsAuthenticated,)
4
5
def get_queryset(self):
6
owner = self.request.user.username
7
return File_Uploader.objects.filter(owner=owner)
8
Advertisement
Answer
I finally got it working using this trick.
Added this field in my model.
JavaScript
1
2
1
is_image = models.BooleanField(default=False)
2
When a user uploads a file I am checking whether the file is image or not using the following code.
JavaScript
1
5
1
if 'image' in request.FILES["file"].content_type:
2
request.data['is_image'] = True
3
else:
4
request.data['is_image'] = False
5
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.