0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Show more descriptive info for references in StreamFields

Show the block path and label to better understand which block has the reference
This commit is contained in:
Sage Abdullah 2023-02-21 16:48:45 +00:00
parent 6398443ad7
commit a857332299
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217

View File

@ -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