reactjs 我无法在Django-Rest-Framework中创建第二个视频

zfycwa2u  于 2022-12-22  发布在  React
关注(0)|答案(1)|浏览(132)

我正在用Django和React创建YouTube克隆。
当用户创建第二个视频时,它会给出错误请求错误:〈查询字典:{'标题':['编辑测试56'],'描述':["是否有效1643“],”用户“:[“3”],“图像”:[〈临时上载文件:图片文件(image/png)〉],'视频':[〈临时上载文件:video-for-fatube.mp4(视频/mp4)〉]}〉错误请求:/API/管理/创建/
当我尝试在postman中进行post请求时,它给了我x1c 0d1x

views.pyAPI的视图。

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)

串行器.py视频串行器。

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视频的模型

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)

如果你想了解整个项目,这里是github:https://github.com/PHILLyaHI/diplom-work

s5a0g9ez

s5a0g9ez1#

您正在使用One2One字段,这就是导致错误的原因,请在用户列中使用外键

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)

相关问题