How to get in django models class Another value of BlogPageGalleryImage image or foreign key or parental key?
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'),
]
It seems you are looking to access the blog page images in your BlogPage
and Another
template. This can be achieved by adding the blog page images to the rendering context. To do this override the get_context
in BlogPage
method:
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
This means that you can now in your template access the images using {{ images }}
. So for example:
{% for i in images %}
<div class="image">
{% image i.image %}
<p>{{ i.caption }}</p>
</div>
{% endfor %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.