Created
April 15, 2019 07:47
-
-
Save gtindo/ab4cbe99e99fb2dccf810a6937286459 to your computer and use it in GitHub Desktop.
Django CsvFileField
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mimetypes | |
from django import forms | |
from django.core import validators | |
class CsvFileField(forms.FileField): | |
def validate(self, value): | |
super().validate(value) | |
file_mime = mimetypes.guess_type(value) | |
if file_mime != 'text/csv': | |
raise validators.ValidationError( | |
('Invalid file type'), | |
code='Invalide' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment