从w外部上传Wa图像 [英] Uploading Wagtail images from outside of wagtail
问题描述
在无法子类化 Page
的Django模型中,我想将现有的ImageField转换为使用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='+'
)
用户需要能够将图像上传到其个人资料视图.在Django视图的forms.py中,我有:
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.
我知道我需要手动将图像附加到UserProfile和Collection,但是需要首先克服该错误.还是尝试在非WT模型中使用各个WT字段是一个坏主意吗?谢谢.
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.
推荐答案
这里的问题是模型上的 avatar
ForeignKey字段期望接收到 wagtailimages.Image的实例.
模型.表单上的 ImageField
无法提供此功能-它仅提供一个文件对象.为了使这项工作有效,您需要设置您的表单(我假设是ModelForm)来与您自己的模型同时创建 wagtailimages.Image
对象.可以使用以下自定义 save
方法执行此操作:
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:
- 将
avatar = forms.ImageField
重命名为与avatar
模型字段不冲突的名称,例如avatar_image
- 确保您的ModelForm的
fields
定义中包含了avatar_image
,但没有包含avatar
.此时,avatar_image
只是表单上的一个额外字段,与模型没有任何关系. -
在ModelForm上定义以下
save
方法:
- Rename
avatar = forms.ImageField
to something that doesn't clash with theavatar
model field, such asavatar_image
- Ensure that
avatar_image
is included in your ModelForm'sfields
definition, butavatar
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
这篇关于从w外部上传Wa图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!