I had requested a file validator
([https://code.djangoproject.com/ticket/34700 #34700]), but you said I
couldn't add it to its core and I have a question that can I add file size
validator?
**This code has been tested**
{{{
@deconstructible
class FileSizeValidator:
"""
file size validator
"""
def __init__(
self,
max_upload_file_size: int = None,
):
"""
:type max_upload_file_size: int
:param max_upload_file_size: If you want the file size to be
checked,
the file size must be in bytes,
example: file_size=1048576 (1MB), defaults to 0, optional
:return: If everything is OK, it will return None, otherwise it
will
return a ValidationError.
"""
if max_upload_file_size is None:
raise ValueError("max_upload_file_size is None, You Must Be
Fill max_upload_file_size ")
self.max_upload_file_size = max_upload_file_size
def __call__(self, value):
current_file = value.file
file_size = current_file.size
if self.max_upload_file_size is not None and file_size >
self.max_upload_file_size:
raise ValidationError("file size not valid")
def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.max_upload_file_size == other.max_upload_file_size
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34704>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => duplicate
Comment:
> I had requested a file validator
([https://code.djangoproject.com/ticket/34700 #34700]), but you said I
couldn't add it to its core and I have a question that can I add file size
validator?
> **This code has been tested**
For me, this is another (4th) duplicate of #34263. You've already
mentioned the size validator there. Please write to the
DevelopersMailingList if you want other opinions, and
[https://docs.djangoproject.com/en/stable/internals/contributing/triaging-
tickets/#closing-tickets follow the triaging guidelines with regards to
wontfix tickets]. **Don't** create more duplicates of the same ticket.
I'll close all of them immediately.
--
Ticket URL: <https://code.djangoproject.com/ticket/34704#comment:1>