我有一个Django rest服务器,它为前端反应的文件列表提供服务器。我想知道我是否可以通过图像过滤这些文件,并只向我的react前端显示图像。我找了很多,但找不到有用的东西。请帮帮我。提前谢谢你。
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)
我终于使用了这个技巧。
在我的模型中添加了此字段。
is_image = models.BooleanField(default=False)
当用户上传文件时,我会使用以下代码检查文件是否为图像。
if 'image' in request.FILES["file"].content_type:
request.data['is_image'] = True
else:
request.data['is_image'] = False
现在,我可以根据这个is_image字段轻松地过滤文件。
如果这不是一个好的解决方案,请告诉我。。
谢谢你复习我的问题。我真的很感激。