开发者

Django - FileField and images

I'm trying to create some kind of 'media manager' model which will allow the user to upload different kings of media (images, swfs, pdfs) similar to the way WordPress does. My media model looks something like this:

class Media(models.Model):
    id = models.AutoField(primary_key=True)
    url = models.FileField(upload_to="uploads")
    mimetype = models.CharField(max_length=64, editable=False)
    created = models.DateTimeField(auto_now_add=True, editable=False)

When a user uploads a file, I want to first determine what kind of file it is and if it's an image, manipulate it further. I want to be able to to specify the dimensions (crop) of the uploaded image via a view, so when I call the .save() method, the model will resize and crop the image, upload it and populate the database with the url to the file.

I also want to ensure that the upload of the image is done AFTER the post processing (cropping etc), I have no need to keep the original file.

So the question 开发者_如何学运维I am asking is how do I got about passing parameters to the FileFields save method (so I can pass dynamic properties for image post processing) and how can I ensure the post processing is done BEFORE the image is uploaded?

Edit: When I say before the image is uploaded, I mean before it's saved to it's final destination. I understand the image has to go int othe tmp folder first before I can post process it. Sorry for the misleading question.

Hope someone can help :)


You cannot do anything before the image is uploaded (because you have nothing to work with).

But if you want modify the image before saving it into db, you can do it in model's save() method, before calling parent's save()

If you are uploading it via admin, override method save_model() in admin.py, ie:

def save_model(self, request, obj, form, change):

        file =  request.FILES.get('url') # name of field
        if file:
            # proceed your code

        return super(AdminClassName, self).save_model(request, obj, form, change)


Here is my code how to change file before actually upload it. I think you should get my idea

 from django.core.files.uploadedfile import InMemoryUploadedFile
#....
#some form
        def clean_avatar(self):
            av = self.cleaned_data['avatar']
            resized = make_avatar(av,65)   # My custom function than returns image
            return  InMemoryUploadedFile(resized, av.field_name, av.name, av.content_type, resized.len, av.charset)

You can read django code for InMemoryUploadedFile "documentation". And in your resize/crop function you should use StringIO not file to save result


How could the processing be done before the image is uploaded? That doesn't make sense. The server doesn't have any access to the file until you upload it.

If you actually want to handle the file before it's saved, you can write a custom upload handler. You can test there whether the file is an image, then crop it appropriately, before saving. (You'll need the Python Imaging Library for both of those tasks.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜