Skip to content Skip to sidebar Skip to footer

Adding A Button To Export To A Csv/xlsx File On Wagtail Dashboard

I am attempting the solution mentioned in this stack overflow post (Adding a button to Wagtail Dashboard) however the solution might be outdated, or at least it doesn't work for me

Solution 1:

I used the same implementation as you. I guess that you are having problems in the def export_csv(self) method here my implementation

from djqscsv.djqscsv import render_to_csv_response


classExportView(IndexView):
    model_admin = Nonedefexport_csv(self) -> dict:
        if (self.model_admin isNone) ornothasattr(
            self.model_admin, "csv_export_fields"
        ):
            data = self.queryset.all().values()
        else:
            data = self.queryset.all().values(*self.model_admin.csv_export_fields)
        return render_to_csv_response(data)

    @method_decorator(login_required)defdispatch(self, request: HttpRequest, *args: list, **kwargs: dict) -> dict:
        super().dispatch(request, *args, **kwargs)
        return self.export_csv()

csv_export_fields can be added to your model admin to specify which fields you want to export

here I am adding the HTML file:

{% extends "modeladmin/index.html" %}

{% block header_extra %}
    {% include 'modeladmin/includes/button.html' with button=view.button_helper.export_button %}
    {{ block.super }}
{% endblock %}

your admin.py

classMyModelAdmin(ModelAdmin, ExportModelAdminMixin): 
    model = MyModel 
    menu_icon = 'tag' 
    menu_order = 200 
    index_template_name = "wagtailadmin/export_csv.html"
    csv_export_fields = [ "field_name","field_name_1", ]
    list_display = ('first_name', 'last_name'') 
    search_fields = ('first_name', 'last_name',)

Post a Comment for "Adding A Button To Export To A Csv/xlsx File On Wagtail Dashboard"