diff --git a/wagtail/models/reference_index.py b/wagtail/models/reference_index.py index 83a5233d3b..42fc6ff4a1 100644 --- a/wagtail/models/reference_index.py +++ b/wagtail/models/reference_index.py @@ -10,6 +10,9 @@ from modelcluster.fields import ParentalKey from modelcluster.models import ClusterableModel, get_all_child_relations from taggit.models import ItemBase +from wagtail.blocks import StreamBlock +from wagtail.fields import StreamField + class ReferenceGroups: """ @@ -499,8 +502,8 @@ class ReferenceIndex(models.Model): """ Returns a string describing the field that this reference was extracted from. - At the moment, this will return the label of the model field that the reference - was extracted from. + For StreamField, this returns the label of the block that contains the reference. + For other fields, this returns the verbose name of the field. """ model_path_components = self.model_path.split(".") field_name = model_path_components[0] @@ -510,6 +513,16 @@ class ReferenceIndex(models.Model): if isinstance(field, models.ManyToOneRel): child_field = field.related_model._meta.get_field(model_path_components[2]) return capfirst(child_field.verbose_name) + elif isinstance(field, StreamField): + label = f"{capfirst(field.verbose_name)}" + block = field.stream_block + block_idx = 1 + while isinstance(block, StreamBlock): + block = block.child_blocks[model_path_components[block_idx]] + block_label = capfirst(block.label) + label += f" → {block_label}" + block_idx += 1 + return label else: try: field_name = field.verbose_name