Некоторые тесты указывают на то, что "serializer.is_valid()" является тем, что сохраняет файл, несмотря на В случае, о котором я говорю, «serializer.is_valid()» является ложным.
Мой идеальный результат заключается в том, что когда неприемлемый файл загружается через среду REST, этот файл отсутствует в Папка MEDIA_ROOT.
Пожалуйста, оставьте ответ/комментарий, если у вас есть какие-либо советы.
Это мой код — Models.py:
Код: Выделить всё
from django.db import models
from apps.common.models import BaseModel
from datetime import datetime
import os
from django.core.exceptions import ValidationError
def file_storage_handler() -> str:
"""This function provides a file path to a Document model's FileField based on the current date. If the file path doesn't exist it creates it."""
currentDate = datetime.now().date()
path = f"uploaded_documents/{currentDate.year}/{currentDate.month}/{currentDate.day}/"
if not os.path.exists("media/"+path):
os.makedirs("media/"+path)
return path
def validate_file(file): # I dont believe that the specifics in this validator are important
fileType = file.name.split('.')[-1]
if not fileType in ['pdf','jpeg','jpg','png','bmp','tiff','heif']:
raise ValidationError(f"The .{fileType} file type is not supported")
class Document(BaseModel): # Note BaseModel is not a factor
file = models.FileField(upload_to=file_storage_handler(), validators=[validate_file])
document_type = models.ForeignKey(DocumentType, on_delete=models.PROTECT) # not important
Код: Выделить всё
# Document Endpoints
@api_view(["GET","POST"])
@parser_classes([MultiPartParser, FormParser])
def documents_general(request):
if request.method == "GET":
#some code, not important
elif request.method == "POST":
serializer = DocumentSerializer(data=request.data)
if serializer.is_valid(raise_exception=True): # I think that this line causes the file to save to the MEDIA_ROOT
documentObject = serializer.save()
on_upload.delay(documentObject.id)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Код: Выделить всё
class DocumentSerializer(serializers.ModelSerializer):
class Meta:
model = Document
fields = '__all__'
def update(self, instance, validated_data): # TBH IDK why this is here, likely not important
"""This method is called when the serializer is saved, here I am updating the fields which should be updated."""
instance.document_type = validated_data.get('document_type', instance.document_type)
instance.save()
return instance
Код: Выделить всё
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ["file", "document_type"]
Подробнее здесь: https://stackoverflow.com/questions/787 ... t-although
Мобильная версия