这两种创建轮播的实现有什么区别?他们似乎都在做同样的事情,但是其中一个明确定义了外键。第一个实现可以通过调用轻松插入,同时第二个实现必须通过 ParentalKey 连接到模型。从本质上讲,在主页上实现轮播展示的更好选择是什么?
class ImageCarouselBlock(blocks.StructBlock):
image = ImageChooserBlock()
caption = blocks.TextBlock(required=False)
page = PageChooserBlock()
class Meta:
icon = 'image'
class CarouselItem(LinkFields):
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
link_url = models.models.ForeignKey(
'wagtailcore.Page',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
caption = models.CharField(max_length=255, blank=True)
panels = [
ImageChooserPanel('image'),
FieldPanel('link_url'),
FieldPanel('caption'),
MultiFieldPanel(LinkFields.panels, "Link"),
]
class Meta:
abstract = True