cannot makemigratoins due to my custom field in


I have been facing an error, it is about the migration model.
I created a custom filed inherited from models.FileField for doing something regarding uploaded file validation, it has been used in incident_File.

you can see in the code below.

One I run make the command ==> python makemigrations , it raised error below.

Error is most likely from my custom field inherited from FileField.

How to fix this issue?

from django.db import models
from django.contrib.auth.models import  User
#from django.contrib.auth import get_user_model

from django.conf import settings

from django.db.models.signals import post_delete
from django.dispatch import receiver

class ContentTypeRestrictedFileField(models.FileField):

    def __init__(self, *args, **kwargs):
        self.content_types = kwargs.pop("content_types")
        self.max_upload_size = kwargs.pop("max_upload_size")

        super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)

    def clean(self, *args, **kwargs):
        data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)

        file = data.file
            content_type = file.content_type
            if content_type in self.content_types:
                y= self.max_upload_size
                if x > y:
                    raise ValidationError((f'File size must be less than {y} {xy}. Current file size is {x} {xy}') )
                raise ValidationError((f'Filetype {content_type} is not supported.'))
        except AttributeError:

        return data
class Incident_File(models.Model):

    incident_file = models.FileField('Upload File',blank=True,max_length=254)
    incident_file = ContentTypeRestrictedFileField('Upload File', upload_to=incident_directory_path, blank=True, max_length=254,

Thank you for your help


class ContentTypeRestrictedFileField(models.FileField):

    def __init__(self, *args, **kwargs):
        self.content_types = kwargs.pop("content_types", None)
        self.max_upload_size = kwargs.pop("max_upload_size", None)

        super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)


class ContentTypeRestrictedFileField(models.FileField):

    def __init__(self, ct=None, mus=None, *args, **kwargs):
        self.content_types = ct
        self.max_upload_size = mus

        super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)

Looking through the examples in the documentation, I get the impression that an instance of the field may be created outside the context of the model in which it is being used, in which case those keyword parameters aren’t available for that instance - causing the KeyError. (But that’s 100% conjecture and not a statement of known fact.)

(Side note: In the future, please copy/paste the text of the stack dump. Images are not always readable on all devices and are impossible to selectively quote in a reply.)

Thank you for all your assistance.

I no longer use ContentTypeRestrictedFileField class.
I move entire logic within the clean method in this class to clean method in modelfrom class instead.

def clean(self, *args, **kwargs):

It works absolutely.