Answer the question
In order to leave comments, you need to log in
Why does an error occur when serializing a file name?
Website in Django.
Dependent packages:
django 3.0.5
djangorestframework 3.11.0
When uploading files with certain names, the serializer gives invalidity. For example, this name crashes the serialization of "2 Sub Urban ft. Rei Ami - Freak (Rus Cover Oxygen1um ft. Lil Key) ▶ Song In Russian - Oxygenium Translation Rus Cover.mp4" (I understand that the title is too "tricky", but nothing I can do it - users are trying to download this)
Serializer code
class ClipVideoFileSerializer(serializers.ModelSerializer):
"""Clip VideoFile Serializer"""
class Meta:
model = Clip
fields = "__all__"
read_only_fields = ('error', 'converted', 'is_playing', 'clip_duration',)
class ClipView(APIView):
"""Clip View"""
parser_classes = (MultiPartParser, FormParser, JSONParser)
object_serializer = ClipSerializer
object_get_serializer = ClipGetSerializer
video_file_serializer = ClipVideoFileSerializer
json_object_name = "clip"
"""
Часть несвязанного с проблемой кода скрыта
"""
def put(self, request):
if request.data.get(self.json_object_name):
""" Часть несвязанного с проблемой кода скрыта """
""" При аплоаде выполняется блок elif """
elif request.data.get("video_file") is not None and request.data.get("id") is not None:
instance = get_object_or_404(Clip.objects.all(), pk=request.data.get("id"))
""" Часть несвязанного с проблемой кода скрыта """
file_serializer = self.video_file_serializer(
instance=instance,
data=request.data,
partial=True
)
if file_serializer.is_valid():
""" Часть несвязанного с проблемой кода скрыта """
else:
data = request.data
return self.get(request, context={"detail": "update error", "id": data.get("id"),
"convertation_status": "error"})
else:
return self.get(request, context={"error": "bad request"})
"""
Часть несвязанного с проблемой кода скрыта
"""
class Clip(models.Model):
"""Clip"""
""" Часть несвязанного с проблемой кода скрыта """
name = models.CharField("Clip name", max_length=150, default="NoName")
video_file = models.FileField("Clip", upload_to="clips", null=True, blank=True)
""" Часть несвязанного с проблемой кода скрыта """
Answer the question
In order to leave comments, you need to log in
Check what error the serializer gives:
print(file_serializer.errors)
In a specific case, the error is in exceeding the length of the file name string:
{'video_file': [ErrorDetail(string='Ensure this filename has at most 100 characters (it has 120).', code='max_length')]}
video_file = models.FileField("Clip", upload_to=get_new_video_file_path, null=True, blank=True, max_length=255)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question