Uploading Wagtail images from outside of wagtail

余生长醉 提交于 2020-01-14 18:46:59

问题


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.


回答1:


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 wagtailimages.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
    


来源:https://stackoverflow.com/questions/42544797/uploading-wagtail-images-from-outside-of-wagtail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!