from django import forms
from book.models import Book
from django.forms import ModelForm


class BookForms(forms.Form):
    class Meta:
        model = Book
        fields = ['name', 'note']


class AddForms(ModelForm):
    class Meta:
        model = Book
        fields = ['name', 'pdf']

from django import forms
from book.models import Book
from django.forms import ModelForm
import magic


class BookForms(forms.Form):
    class Meta:
        model = Book
        fields = ['name', 'note']


class AddForms(ModelForm):

    def clean_file(self):
        pdf = self.cleaned_data['pdf']
        mime = magic.from_buffer(pdf.read(), mime=True)
        print(mime)
        if not mime == 'application/pdf':
            raise forms.ValidationError('File must be a PDF document')
        else:
            return pdf

    class Meta:
        model = Book
        fields = ['name', 'pdf']
Add file validation