Skip to main content

Django validators used for commonly used validations

Project description

Django Validators

Django validators provides validator function that can be used with Django form fields and Django rest framework serializer fields

Installation

Install django-validators using pip

pip install django-validators

Types of validators

File validators

  • Mime type validator djangovalidators.validators.MimetypeValidator

    This validator is used to check mime type/ content type of the uploaded file. This validator checks file header information to check mime type on FileField

    example

    from djangovalidators.validators import MimetypeValidator
    
    # with django forms
    class DemoForm(forms.Form):
        CONTENT_TYPES = ('image/png', 'application/pdf', 'image/jpeg', 'image/jpg', 'image/tiff',)
        file_object = forms.FileField(validators=[MimetypeValidator(CONTENT_TYPES)])
    
    # with django serializers
    class DemoSerializer(serializers.Serializer):
        CONTENT_TYPES = ('image/png', 'application/pdf', 'image/jpeg', 'image/jpg', 'image/tiff',)
        FILE_SIZE_IN_BYTES = 1024 * 1024 * 5  # ~5 Mib
        file_object = serializers.FileField(validators=[MimetypeValidator(CONTENT_TYPES)])
    
  • File size validator djangovalidators.validators.FileSizeValidator

    This validator is useful for validating file size in FileField

    example

    from djangovalidators.validators import FileSizeValidator
    
    # with django forms
    class DemoForm(forms.Form):
        FILE_SIZE_IN_BYTES = 1024 * 1024 * 5  # ~5 Mib
        file_object = forms.FileField(validators=[FileSizeValidator(FILE_SIZE_IN_BYTES)])
    
    # with django serializers
    class DemoSerializer(serializers.Serializer):
        FILE_SIZE_IN_BYTES = 1024 * 1024 * 5  # ~5 Mib
        file_object = serializers.FileField(validators=[FileSizeValidator(FILE_SIZE_IN_BYTES)])
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-validators, version 1.0.1
Filename, size File type Python version Upload date Hashes
Filename, size django_validators-1.0.1-py3-none-any.whl (3.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-validators-1.0.1.tar.gz (2.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page