H0lyBruh
H0lyBruh

Reputation: 1

Django REST framework serializer.is_valid() saves files to MEDIA_ROOT although .is_valid() is False

I have a project which supports file uploads via Django forms and also Django REST framework, these files are used are stored in a model 'Document'. The issue is that when a file that fails validation is uploaded via the REST framework the file is still saved to its 'upload_to' parameter (MEDIA_ROOT) (No 'Document' model instance is created as expected), this doesn't happen when uploading the same file via Django forms.

Some testing seems to point to "serializer.is_valid()" being the thing that saves the file despite "serializer.is_valid()" is false in the case I am referring to.

My ideal outcome is that when a unacceptable file is uploaded via REST framework, that file is not present in the MEDIA_ROOT folder.

Please leave a answer/comment if you have any advice.

This is my code - 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

views.py:

# 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)

Serializer.py:

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

forms.py: (prob not important)

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document
        fields = ["file", "document_type"]

Please let me know if you have advice or questions.

Upvotes: 0

Views: 43

Answers (0)

Related Questions