1

I'm new to django and I hope you can guide me with this, I have a model definition like this:

class Documents(models.Model): 
    name = models.CharField(max_length=255)
    date = models.DateField()
    path = models.FileField(upload_to='documents/')

But I need to validate it as a valid PDF file, could you guide me how to do that?

1 Answer 1

1

You should validate file upon upload. Here's a simple solution using custom model field: http://djangosnippets.org/snippets/2206/

So your code would be:

...
path = ContentTypeRestrictedFileField(
    upload_to='documents/',
    content_types = ['application/pdf'])
....

It's a good start even if you want to create more complex validation, since this one uses only file extension checking, I guess.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.