如何进入django模型类BlogPageGalleryImage图像或外键或父母键的另一个值?
class BlogPage(Page):
date = models.DateField("Дата публикации" , default=datetime.date.today )
body = RichTextField(blank=True)
name = models.CharField(max_length=80, default='Лучший')
class BlogPageGalleryImage(Orderable):
page = ParentalKey(BlogPage, related_name='gallery_images')
image = models.ForeignKey('wagtailimages.Image', on_delete=models.CASCADE, related_name='+')
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
]
class Another(BlogPage):
page = ParentalKey(BlogPage, related_name='instagram_post')
Image = BlogPageGalleryImage.objects.get(BlogPageGalleryImage.)
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
]
答案 0 :(得分:1)
您似乎希望在BlogPage
和Another
模板中访问博客页面图片。这可以通过将博客页面图像添加到渲染上下文来实现。要执行此操作,请覆盖get_context
方法中的BlogPage
:
def get_context(self, request, *args, **kwargs):
context = super(BlogPage, self).get_context(
request, *args, **kwargs
)
context['images'] = BlogPageGalleryImage.objects.filter(
page=self,
)
return context
这意味着您现在可以在模板中使用{{ images }}
访问图像。例如:
{% for i in images %}
<div class="image">
{% image i.image %}
<p>{{ i.caption }}</p>
</div>
{% endfor %}