shacker
shacker

Reputation: 15371

Uploading Wagtail images from outside of wagtail

In a Django model that cannot subclass Page, I want to convert an existing ImageField to use Wagtail images. I have redefined the field as:

avatar = models.ForeignKey(
    'wagtailimages.Image', null=True, on_delete=models.SET_NULL, related_name='+'
)

The user needs to be able to upload an image to their profile view. In forms.py for the Django view, I have:

avatar = forms.ImageField(
    label='Your Photo', required=False,
    error_messages={'invalid': "Image files only"}, widget=forms.FileInput())

When I upload an image to the view, it crashes on:

Cannot assign "<InMemoryUploadedFile: filename.jpg (image/jpeg)>":
"UserProfile.avatar" must be a "Image" instance.

I'm pretty sure the problem is the field definition in the form, but I can't figure out what the right definition should be.

I know that I'll need to manually attach the image to the UserProfile and the Collection, but need to get past this error first. Or is it a bad idea to try and use individual WT fields in a non-WT model? Thanks.

Upvotes: 3

Views: 3019

Answers (1)

gasman
gasman

Reputation: 25292

The problem here is that the avatar ForeignKey field on your model is expecting to receive an instance of the wagtailimages.Image model. The ImageField on the form isn't capable of providing this - it only provides a file object. To make this work, you'll need to set up your form (which I'm assuming is a ModelForm) to create the wagtailimages.Image object at the same time as your own model. It should be possible to do this with a custom save method as follows:

  • Rename avatar = forms.ImageField to something that doesn't clash with the avatar model field, such as avatar_image
  • Ensure that avatar_image is included in your ModelForm's fields definition, but avatar isn't. At this point, avatar_image is just an extra field on the form, with no connection to the model.
  • Define the following save method on the ModelForm:

    from wagtail.images.models import Image
    
    def save(self, commit=False):
        if not commit:
            raise Exception("This form doesn't support save(commit=False)")
    
        # build the instance for this form, but don't save it to the db yet
        instance = super(MyForm, self).save(commit=False)
    
        # create the Image object
        avatar = Image.objects.create(
            file=self.cleaned_data['avatar_image'],
            title="Image title"
            # you may need to add more fields such as collection to make it a valid image...
        )
    
        # attach the image to your final model, and save
        instance.avatar = avatar
        instance.save()
        return instance
    

Upvotes: 10

Related Questions