Django - how do you turn an InMemoryUploadedFile into an ImageField's FieldFile?

24,412

Solution 1

You need to save the InMemoryUploadedFile to the ImageField rather than 'turning' it into an ImageField:

image = request.FILES['img']
foo.imagefield.save(image.name, image)

where foo is the model instance, and imagefield is the ImageField.

Alternatively, if you are pulling the image out of a form:

image = form.cleaned_data.get('img')
foo.imagefield.save(image.name, image)

Solution 2

You trying to do it in ModelForm?

This is how i did for file field

class UploadSongForm(forms.ModelForm):
    class Meta:
        model = Mp3File

    def save(self):
        content_type = self.cleaned_data['file'].content_type
        filename = gen_md5() + ".mp3"
        self.cleaned_data['file'] = SimpleUploadedFile(filename, self.cleaned_data['file'].read(), content_type)
        return super(UploadSongForm, self).save()

You can take it as example and look in source what InMemoryUploadedFile class needs in initialization parameters.

Solution 3

You could implement a form with a file upload field by using form instances, here is the view:

def form_view(request):
    if request.method == 'POST':
        form = FooForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return render_to_response('result.html')
        return render_to_response('form.html', {
            'form': form;
            'error_messages': form.errors;
        }
    form = FooForm()
    return render_to_response('form.html', {
        'form': form;
    }

form.save() saves the uploaded file along with all the other fields as you included request.FILES argument in it's constructor. In your models you have to define FooForm subclass of ModelForm class like this:

class FooForm(ModleForm):
    Meta:
        model = Foo

...where Foo is the subclass of Model, that describes the data you want to store persistently.

Share:
24,412
Louis Sayers
Author by

Louis Sayers

uxu.io

Updated on March 03, 2020

Comments

  • Louis Sayers
    Louis Sayers about 4 years

    I've been trying help(django.db.models.ImageField) and dir(django.db.models.ImageField), looking for how you might create a ImageField object from an image that is uploaded.

    request.FILES has the images as InMemoryUploadedFile, but I'm trying to save a model that contains an ImageField, so how do I turn the InMemoryUploadedFile into the ImageField?

    How do you go about finding this type of thing out? I suspect that the two classes have an inheritance relationship, but I'd have to do lots of dir() -ing to find out if I were to look.

  • Louis Sayers
    Louis Sayers over 15 years
    Thanks, I'll have a look into your example closer tomorrow. I'm bypassing the forms process, as I'm trying to save users from uploading images twice, so can't do the cleaned_data thing.
  • Imnl
    Imnl over 7 years
    This answer is not correct. Yo can use the file content as you wish without saving it in a model instance. Inspect the file attribute
  • remort
    remort over 5 years
    Perfect! Thanks. You saved my time.
  • Omid Reza Abbasi
    Omid Reza Abbasi almost 4 years
    What if the form is not binded to a model? Actually it expects a File, not an InMemoryUploadedFile, and the form is not validated.