Home > database >  I can't Create Second Video in the Django-Rest-Framework
I can't Create Second Video in the Django-Rest-Framework

Time:12-22

I'm using the Django and React to create YouTube clone.

And when the user is creating second video, it giving the bad request error: <QueryDict: {'title': ['Edit Test56'], 'description': ['is It working1643'], 'user': ['3'], 'image': [<TemporaryUploadedFile: slackphot.png (image/png)>], 'video': [<TemporaryUploadedFile: video-for-fatube.mp4 (video/mp4)>]}> Bad Request: /api/admin/create/

When I tried to make post request in postman, it gave me that error

views.py Views of the api.

class CreateVideo(APIView):
    #permissions_classes = [IsAuthenticated]
    parser_classes = [MultiPartParser, FormParser]

    def post(self, request, format=None):
        print(request.data)
        serializer = VideoSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

serializer.py Video Serailzier.

class VideoSerializer(serializers.ModelSerializer):
    user_name = CharField(source="user.user_name", read_only=True)

    class Meta:
        model = Video
        fields = ["id", "title", "image", "video", "description", "date_added", "is_active", "user", "user_name", "likes"]

models.py Model of the Video

class Video(models.Model):
    title = models.CharField(max_length=50)
    image = models.ImageField(_("Image"), upload_to=upload_to, default="videos/default.jpg")
    video = models.FileField(_("Video"), upload_to=upload_to, default="videos/default.mp4")
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="user_channel"
    )
    description = models.CharField(max_length=255)
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='video_post', null=True, blank=True)
    date_added = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)

If you want to look at the whole project, here is github: https://github.com/PHILLyaHI/diplom-work

CodePudding user response:

You are using One2One Field, that's why it's causing an error, use foreign key in User column

class Video(models.Model):
    title = models.CharField(max_length=50)
    image = models.ImageField(_("Image"), upload_to=upload_to, default="videos/default.jpg")
    video = models.FileField(_("Video"), upload_to=upload_to, default="videos/default.mp4")
    user = models.ForiegnKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="user_channel"
    )
    description = models.CharField(max_length=255)
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='video_post', null=True, blank=True)
    date_added = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)

  • Related