Wagtail (Django CMS) 如何在 wagtail 中添加单个图像,简单的尝试以 "OperationalError no such column: gallery_gallerysubpage.cover_id" 结束
Wagtail (Django CMS) how to add single image in wagtail, simple attempt ends with "OperationalError no such column: gallery_gallerysubpage.cover_id"
我的问题很简单,我只是不知道如何将单个图像添加为页面缩图。这是我的页面模型(我试过 ImageChooserPanel reference 所示):
class GallerySubpage(Page):
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
# THIS IS TAKEN FROM DOCS
cover = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
# This returns some dummy for a while. I want to replace this with some field that contain single Image
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
FieldPanel('intro'),
FieldPanel('body', classname="full"),
InlinePanel('gallery_images', label = "Images that will be displayed on this page"),
# THIS IS TAKEN FROM DOCS
ImageChooserPanel('cover'),
]
当我 运行 代码时出现错误:
OperationalError at /gallery/galerry132/
no such column: gallery_gallerysubpage.cover_id
如果在添加cover
字段后没有运行./manage.py makemigrations
和./manage.py migrate
就会出现上述错误。
更改模型后,重新运行以下命令使其生效:
- python manage.py makemigration
- python manage.py 迁移
- python manage.py 运行服务器
我的问题很简单,我只是不知道如何将单个图像添加为页面缩图。这是我的页面模型(我试过 ImageChooserPanel reference 所示):
class GallerySubpage(Page):
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
# THIS IS TAKEN FROM DOCS
cover = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
# This returns some dummy for a while. I want to replace this with some field that contain single Image
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
FieldPanel('intro'),
FieldPanel('body', classname="full"),
InlinePanel('gallery_images', label = "Images that will be displayed on this page"),
# THIS IS TAKEN FROM DOCS
ImageChooserPanel('cover'),
]
当我 运行 代码时出现错误:
OperationalError at /gallery/galerry132/
no such column: gallery_gallerysubpage.cover_id
如果在添加cover
字段后没有运行./manage.py makemigrations
和./manage.py migrate
就会出现上述错误。
更改模型后,重新运行以下命令使其生效: - python manage.py makemigration - python manage.py 迁移 - python manage.py 运行服务器