Update a field of a Django Model class from another class linked through a OneToOne Relationship

I'm on Django 5.0.4. I'm building an API using DRF. I'm new to DRF.

I have 2 django models which looks like this :

class PostVideo(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='videos', null=True)
    width = models.PositiveIntegerField(editable=False, null=True)
    height = models.PositiveIntegerField(editable=False, null=True)
    duration = models.FloatField(editable=False, null=True)
    file = VideoField(width_field='width', height_field='height',
                      duration_field='duration', storage=post_image_storage,
                      upload_to=upload_to_post_video_directory, blank=False, null=True)
    
    video_type = (('regularvideo', 'regularvideo'),('gridvideo', 'gridvideo'),('livevideostream', 'livevideostream'))
    mediaviewtype = models.CharField(max_length=24, choices=video_type, default='')

class LiveVideoStream(models.Model):
    parent = models.OneToOneField(Post, related_name='live_video_stream', blank=False, null=True,on_delete=models.CASCADE)
    stream_uuid = models.CharField(_('uuid'), null=False, editable=False, unique=False,
                                   max_length=settings.LIVE_VIDEO_STREAM_UUID_MAX_LENGTH)
    streamer = models.ForeignKey(User, related_name='live_video_streams', on_delete=models.CASCADE)
    started_at = models.DateTimeField(
        default=timezone.now, blank=False, editable=False,null=False)
    video = models.OneToOneField(PostVideo, related_name='live_video_stream', blank=False, null=True,on_delete=models.CASCADE)
    
    
    def create_live_video_stream_post(cls, parent, stream_uuid, streamer,started_at):
               
        return cls.objects.create(parent=parent,stream_uuid=stream_uuid,streamer=streamer, 
                                  started_at=started_at)

My objective is to update the mediaviewtype in the PostVideo class with the value ‘livevideostream’ when i create a PUT request to create a Live Video Stream Post using the create_live_stream_post method above. I’m not sure how to do it.I get the value of mediaviewtype which is 'livevideostream' from my frontend flutter app and I process it via a Serializer, but I get stuck when I think how to update the value of mediaviewtype in model PostVideo.*

The view to create a live stream post :

def put(self, request):
        query_params = request.data.dict()
        serializer = LiveVideoStreamRequestSerializer(data=query_params)
        serializer.is_valid(raise_exception=True)

        data = serializer.validated_data

        post_uuid = data.get('post_uuid')
        streamer = data.get('streamer')
        started_at = data.get('started_at')
        media_view_type = data.get('media_view_type')
        
        User = get_user_model()
        user = request.user

        if streamer=='':
            streamer = user
        else:
            streamer = User.objects.get(username=streamer)

        Post = get_post_model()
        post = Post.objects.get(uuid=post_uuid)
        Stream = get_live_video_stream_model()

        # Create Live Stream Post.
        live_video_stream = Stream.create_live_video_stream_post(parent=post,stream_uuid=post_uuid, streamer=streamer,
                                                                 started_at=started_at)

        return Response(LiveVideoStreamResponseSerializer(live_video_stream, context={"request": request}).data, status=status.HTTP_200_OK)
The request & response serializers : 

class LiveVideoStreamRequestSerializer(serializers.Serializer):    
    post_uuid = serializers.CharField(required=True)
    streamer=serializers.CharField(max_length=50, required=False)
    started_at = serializers.DateTimeField(required=False)
    mediaviewtype = serializers.CharField(max_length=50, required=True)

class LiveVideoStreamResponseSerializer(serializers.ModelSerializer):
    parent = UserPostSerializer(read_only=True)
    stream_uuid = serializers.CharField(read_only=True)

    class Meta:
        model = LiveVideoStream
        fields = (
            'id',
            'parent',
            'stream_uuid',
            'streamer',            
            'started_at',
            'mediaviewtype',
        )
Вернуться на верх