8000 Custom field access in Wagtail ReportView needs official documentation · Issue #13103 · wagtail/wagtail · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Custom field access in Wagtail ReportView needs official documentation #13103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ASneakyToast opened this issue May 10, 2025 · 0 comments
Open

Comments

@ASneakyToast
Copy link

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:

  1. How to prefix field names with custom. in the list_export attribute
  2. How to set up corresponding entries in export_headings
  3. How to configure custom_field_preprocess to specify processing methods
  4. 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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant
0