0

I have the following model in my models.py file:

class Video(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    text = models.CharField(max_length=100, blank=True)
    video = models.FileField(upload_to='Videos/',
                              blank=True)
    gif = models.FileField(upload_to='Images/',
                              blank=True)

    class Meta:
        ordering = ('created', )

Before Django creates an instance of this model, I want to set the gif field on the Django site. So, the data comes to Django from the client site with the field video set, and based on the video field's content I want to fill the gif field.

For that, which create() method do I need to override for this task? The create() method of my VideoSerializer class in serializers.py :

def create(self, validated_data):
        return Video(**validated_data)

Or the create() method of my VideoCreate class in views.py:

class VideoCreate(generics.CreateAPIView):
    queryset = Video.objects.all()
    serializer_class = VideoSerializer
    parser_classes = (MultiPartParser, )

    def create(self, request, *args, **kwargs):

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

Hope someone can help

ebeninki
  • 909
  • 1
  • 12
  • 34
  • In terms of result, it doesn't matter. Both will work. From an architecture point of view, I'd use the `Model`'s `save()` method for this. It's the model that should know how to save itself, check whether or not the `gif` exists and add it if needed. – dirkgroten Mar 25 '20 at 11:43
  • How would that look like ? In the django rest framework documentation I could not find the save() method – ebeninki Mar 25 '20 at 11:51
  • The `Model` I said. Look at the django docs for `Model` – dirkgroten Mar 25 '20 at 12:02
  • ok. I used it. But now I get the following error I described here: https://stackoverflow.com/questions/60852102/django-trying-to-create-a-gif-from-uploaded-video – ebeninki Mar 25 '20 at 16:27

0 Answers0