Django Rest Framework保存到具有ImageField的模型

时间:2019-06-26 13:37:04

标签: django django-rest-framework

我有以下模型:

class ProgressImage(TimeStampedModel):
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='progressimages')
    image = models.ImageField(
        upload_to=obfuscate_filename, storage=storage, null=True)

这是序列化程序类:

class ProgressImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProgressImage
        fields = ('image', 'created')

这是视图集类:

class ProgressImageViewSet(viewsets.ModelViewSet):
    serializer_class = ProgressImageSerializer
    queryset = ProgressImage.objects.all()
    filter_backends = (IsOwnerFilterBackend,)
    http_method_names = ['get', 'post']
    permission_classes = (IsAuthenticated,)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

我不了解的是如何执行图像保存。可能在视图集中的某个位置,但是我不知道如何。有人知道吗?

0 个答案:

没有答案