2

From everything I can find, the FileField.clean() method should be executed when a file is added to a model, but the method is simply never executed.

Note that I'm referring to the models.FileField object, not forms.FileField. See: related stackoverflow question

I am looking to validate that a file saved in models.FileField is of a certain file type and below a specified size. This is using the Django Rest Framework. The clean() method is never called on save. Why not?

View:

class FileUploadCreate(generics.CreateAPIView):

    serializer_class = FileUploadSerializer

    def get_queryset(self):
        return FileUpload.objects.filter()

    def perform_create(self, serializer):

        upload = self.request.data['file']

        instance = serializer.save(
           name='Name',
           datafile=upload,
        )

        instance.save()            

Model:

class ContentTypeRestrictedFileField(models.FileField):

    def __init__(self, *args, **kwargs):
        # Log that it hits here
        super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)

    def clean(self, *args, **kwargs):
        print("I NEVER MAKE IT HERE")
        data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)
        file = data.file
        try:
            content_type = file.content_type
            if content_type in self.content_types:
                if file._size > self.max_upload_size:
                    raise ValidationError('Too big')
            else:
                raise ValidationError('Filetype not supported.')
        except AttributeError:
            pass

        return data


class FileUpload(BaseModel):
    name = models.CharField(max_length=128, blank=True, null=True)
    datafile = ContentTypeRestrictedFileField(content_types=['video/x-msvideo', 'application/pdf', 'video/mp4', 'audio/mpeg', ], max_upload_size=1024)
7
  • what exactly you need from that process? such as; rename file, make a validator, or what? Commented Mar 1, 2017 at 15:16
  • I'm looking to validate and restrict of file content type and file size. This is via the django-rest-framework, so there are no formfields. Commented Mar 1, 2017 at 15:21
  • are you following this answer? stackoverflow.com/a/9016664/6396981 Commented Mar 1, 2017 at 15:32
  • 1
    What exactly is your question? What do you mean by "when a file is added to a model"? That's ambiguous. Please add a minimal reproducible example Commented Mar 1, 2017 at 15:37
  • 1
    Try adding the line instance.full_clean() before instance.save(). Commented Mar 1, 2017 at 16:15

1 Answer 1

0

is_valid() method automatically executes clean() and does validation in django.

For instance

form = some_form(request.POST)
if form.is_valid():
    # clean() is automatically executed
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.