I have created a custom photoBlock to embed photos in a blog post. I am trying to create a template tag that would allow me to get every photoBlock that is stored in the database, almost like you query pages in Wagtail. I would prefer not to have to create a separate page for every photo, but instead be able to display them on a different page if possible via the template tag.
class photoBlock(blocks.StructBlock):
date = blocks.DateBlock("Date Image Was Taken")
location = blocks.CharBlock(max_length=100)
image = ImageChooserBlock()
latitude = blocks.DecimalBlock(max_digits=9, decimal_places=6)
longitude = blocks.DecimalBlock(max_digits=9, decimal_places=6)
description = blocks.RichTextBlock()
I am able to get the following to work, but it feels like the query is very heavy and will get bogged down once I have a bunch of posts.
for page in BlogPage.objects.all():
for block in page.body.stream_data:
if block['type'] == 'photoBlock':
return block
Is there any better way to query a specific block from Streamfield? Anytime I try something like this
photoBlock.objects.all()
I always get an error response that photoBlock doesn't have attribute Object. Any ideas?