Skip to content

Instantly share code, notes, and snippets.

@gtindo
Created April 15, 2019 07:47
Show Gist options
  • Save gtindo/ab4cbe99e99fb2dccf810a6937286459 to your computer and use it in GitHub Desktop.
Save gtindo/ab4cbe99e99fb2dccf810a6937286459 to your computer and use it in GitHub Desktop.
Django CsvFileField
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