Skip to content

Admin Actions

actions

export_as_csv_action(description='Export selected as CSV file', fields=None, exclude=None, header=True)

Return a Django admin action that exports selected rows as a CSV file.

Works like Django ModelForm field selection: specify fields to include only those columns, or exclude to remove specific columns. If neither is given, all model fields are exported.

Parameters:

Name Type Description Default
description

Short description shown in the admin actions dropdown.

'Export selected as CSV file'
fields

List of field names (or related lookups) to include. When provided, columns appear in the given order.

None
exclude

List of field names to exclude. Ignored if fields is set.

None
header

If True, the first CSV row contains column headers using the field's verbose_name.

True

Returns:

Type Description

An admin action callable suitable for actions = [export_as_csv_action()].

Example
class MyAdmin(admin.ModelAdmin):
    actions = [export_as_csv_action(
        description="Download CSV",
        fields=['first_name', 'last_name', 'email'],
    )]