Hi
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 manage.py 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
try:
content_type = file.content_type
if content_type in self.content_types:
content_size=file.size
x=round((content_size/1024/1024),2)
y= self.max_upload_size
xy=settings.UPLOAD_FILE_UNIT
if x > y:
raise ValidationError((f'File size must be less than {y} {xy}. Current file size is {x} {xy}') )
else:
raise ValidationError((f'Filetype {content_type} is not supported.'))
except AttributeError:
pass
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,
content_types=settings.UPLOAD_FILE_TYPES,max_upload_size=settings.UPLOAD_FILE_MAX_SIZE_MB)
Thank you for your help
Pongthorn
