Custom field access in Wagtail ReportView needs official documentation · Issue #13103 · wagtail/wagtail · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The documentation for Wagtail's ReportView currently lacks information on how to include custom computed fields in report exports. There's a powerful pattern involving the custom. prefix that allows developers to add dynamically generated fields to reports, but this isn't documented anywhere.
I'd like to add a new subsection under "Adding reports" titled "Adding custom fields to report exports" which documents:
How to prefix field names with custom. in the list_export attribute
How to set up corresponding entries in export_headings
How to configure custom_field_preprocess to specify processing methods
The critical step of overriding the to_row_dict method to handle these custom fields correctly
Example code to be included in the documentation:
from collections import OrderedDict
from django.urls import reverse
from wagtail.admin.views.reports import ReportView
from wagtail.coreutils import multigetattr
class CustomReportView(ReportView):
list_export = [
'title',
'created_at',
'custom.edit_link', # Custom field with the custom. prefix
]
export_headings = {
'title': 'Title',
'created_at': 'Created Date',
'custom.edit_link': 'Edit URL',
}
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Define processors for custom fields
self.custom_field_preprocess = {
"custom.edit_link": {
self.FORMAT_CSV: self.get_edit_link,
self.FORMAT_XLSX: self.get_edit_link,
}
}
def get_edit_link(self, obj):
"""Generate an edit link for the object"""
edit_url = reverse('wagtailadmin_pages:edit', args=(obj.id,))
return self.request.build_absolute_uri(edit_url)
def to_row_dict(self, item):
"""Override to handle custom fields with the custom. prefix"""
return OrderedDict(
(field, multigetattr(item, field))
if not field.startswith("custom.")
else (field, item)
for field in self.list_export
)
Working on this
Happy to submit this documentation change, but I think there's gotta be an easier way to implement custom fields without needing to override to_row_dict. This would be my first contribution, so my experience is limited, but I'd love try and implement this improvement with a little guided direction.
Related links
(Slack support thread)[https://wagtailcms.slack.com/archives/C81FGJR2S/p1746821372602109]
The text was updated successfully, but these errors were encountered:
Pertinent section of the Wagtail docs
https://docs.wagtail.org/en/stable/extending/adding_reports.html#reusing-the-functionality
Details
The documentation for Wagtail's ReportView currently lacks information on how to include custom computed fields in report exports. There's a powerful pattern involving the custom. prefix that allows developers to add dynamically generated fields to reports, but this isn't documented anywhere.
I'd like to add a new subsection under "Adding reports" titled "Adding custom fields to report exports" which documents:
Example code to be included in the documentation:
from collections import OrderedDict
from django.urls import reverse
from wagtail.admin.views.reports import ReportView
from wagtail.coreutils import multigetattr
class CustomReportView(ReportView):
list_export = [
'title',
'created_at',
'custom.edit_link', # Custom field with the custom. prefix
]
Working on this
Happy to submit this documentation change, but I think there's gotta be an easier way to implement custom fields without needing to override to_row_dict. This would be my first contribution, so my experience is limited, but I'd love try and implement this improvement with a little guided direction.
Related links
(Slack support thread)[https://wagtailcms.slack.com/archives/C81FGJR2S/p1746821372602109]
The text was updated successfully, but these errors were encountered: