I would like to save the file mime type by getting it on pre_save signal.
from django.db.models.signals import pre_save
from django.db import models
import magic
class Media (models.Media):
file = models.FileField()
content_type = models.CharField(max_length=128, editable=False)
def media_pre_save(sender, instance, *args, **kwargs):
if not instance.content_type:
mime = magic.Magic(mime=True)
instance.content_type = mime.from_buffer(instance.file.read())
pre_save.connect(media_pre_save, sender=Media)
But I'm getting application/x-empty
when I view it in db. What am I doing wrong?