Skip to content

Instantly share code, notes, and snippets.

@andreasnuesslein
Last active December 18, 2024 11:16
Show Gist options
  • Save andreasnuesslein/7abee778be1c0869f14e153d8f91f212 to your computer and use it in GitHub Desktop.
Save andreasnuesslein/7abee778be1c0869f14e153d8f91f212 to your computer and use it in GitHub Desktop.
Find wagtail Pages that have specific block types in their Streamfields
from django.core.management.base import BaseCommand
from wagtail.models import Page
SF_CLASS_NAMES = [
"StreamField",
"TranslationStreamField",
]
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("blocktype", type=str)
def handle(self, *args, **options):
blocktype = options["blocktype"]
for page in Page.objects.all().specific():
for field in page._meta.fields:
if field.__class__.__name__ not in SF_CLASS_NAMES:
continue
sfield = getattr(page, field.name)
if f"'type': '{blocktype}'" in str(sfield.raw_data):
print(page, page.get_url())
@GorlikItsMe
Copy link

GorlikItsMe commented Dec 18, 2024

The only thing I changed is Page import to from wagtail.models import Page.
Maybe not all options are in SF_CLASS_NAMES list but I dont care because I use StreamField everywhere.
For me works 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment