1

I have a FileField like this:

class Post(models.Model):
    file = models.FileField(null=True, blank=True)
    video = models.BooleanField(default=False)

I'm then able to look at the object when it goes past my views:

def post(request):
    if request.user.is_authenticated():
        form_post = FileForm(request.POST or None, request.FILES or None)
        if form_post.is_valid():
            instance = form_post.save(commit=False)
            if ?something?:
                instance.video = True
            instance.save()

So in my view is it possible to check whether the Post.file is a video or image?

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • 1
    Is checking by extension enough, or do you want to check the file itself? – TemporalWolf Feb 22 '17 at 21:25
  • If extension is enough to determine if it's a video or image then that's fine – Zorgan Feb 22 '17 at 21:27
  • people can put whatever they want as the extension, although most will be labelled appropriately. The other way is via file inspection. – TemporalWolf Feb 22 '17 at 21:35
  • 1
    Possible duplicate of [How to check type of files without extensions in python?](http://stackoverflow.com/questions/10937350/how-to-check-type-of-files-without-extensions-in-python) – TemporalWolf Feb 22 '17 at 21:35
  • How would that look in my code? As i'm checking the filetype in my view as it's being posted uploaded – Zorgan Feb 22 '17 at 22:04

1 Answers1

0

You can check the mime type of the file by accessing the content_type attribute of the file in your view.

turbotux
  • 422
  • 2
  • 11