Last active
December 18, 2024 11:16
-
-
Save andreasnuesslein/7abee778be1c0869f14e153d8f91f212 to your computer and use it in GitHub Desktop.
Find wagtail Pages that have specific block types in their Streamfields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 useStreamField
everywhere.For me works 😎