def METHOD_NAME(self, streamfield_name):
    # Ensure the given streamfield_name is a StreamField on this queryset's
    # model.
    self._check_field(streamfield_name)
    # The following SQL will construct a temporary table intended to be
    # used as part of a larger query that will contain the page id,
    # block index, and block JSON object for each block within the JSON
    # array contained within the given StreamField. This uses PostgreSQL's
    # native JSON support for this construction.
    #
    # Note: this uses .format() instead of params to pass in the database
    # database_table, primary_key_field, and streamfield_name, because the
    # values for database_table and primary_key_field come from the model
    # attached to this QuerySet object itself, and the streamfield_name is
    # validated via the _check_field() method.
    #
    # Passing them through params creates quote formatting that doesn't
    # work with this query.
    return """
        WITH RECURSIVE blocks AS (
            SELECT page_id, index::text, block FROM (
                SELECT
                    {primary_key_field} AS page_id,
                    {streamfield_name}::jsonb AS data FROM {database_table}
            ) x
            LEFT JOIN LATERAL jsonb_array_elements(x.data)
            WITH ORDINALITY AS a (block, index)
            ON true
            UNION ALL
            SELECT
                page_id,
                index || '.' || COALESCE(obj_key, (arr_key - 1)::text),
                COALESCE(arr_block, obj_block)
            FROM blocks
            LEFT JOIN LATERAL
                jsonb_array_elements(
                    CASE jsonb_typeof(block)
                        WHEN 'array' THEN block
                    END
                )
                WITH ORDINALITY as a(arr_block, arr_key)
                ON jsonb_typeof(block) = 'array'
            LEFT JOIN LATERAL
                jsonb_each(
                    CASE jsonb_typeof(block)
                        WHEN 'object' THEN block
                    END
                )
                AS o(obj_key, obj_block)
                ON jsonb_typeof(block) = 'object'
            WHERE arr_key IS NOT NULL OR obj_key IS NOT NULL
        )
    """.strip().format(
        database_table=self.model._meta.db_table,
        primary_key_field=self.model._meta.pk.column,
        streamfield_name=streamfield_name,
    )