Writing an exporter plugin

An Exporter is a method to export the product and order data in pretix for later use in another program.

In this document, we will walk through the creation of an exporter output plugin step by step.

Please read Creating a plugin first, if you haven’t already.

Exporter registration

The exporter API does not make a lot of usage from signals, however, it does use a signal to get a list of all available exporters. Your plugin should listen for this signal and return the subclass of pretix.base.exporter.BaseExporter that we’ll provide in this plugin:

1
2
3
4
5
6
7
8
9
from django.dispatch import receiver

from pretix.base.signals import register_data_exporters


@receiver(register_data_exporters, dispatch_uid="exporter_myexporter")
def register_data_exporter(sender, **kwargs):
    from .exporter import MyExporter
    return MyExporter

The exporter class

class pretix.base.exporter.BaseExporter

The central object of each exporter is the subclass of BaseExporter.

BaseExporter.event

The default constructor sets this property to the event we are currently working for.

BaseExporter.identifier

A short and unique identifier for this exporter. This should only contain lowercase letters and in most cases will be the same as your package name.

This is an abstract attribute, you must override this!

BaseExporter.verbose_name

A human-readable name for this exporter. This should be short but self-explaining. Good examples include ‘JSON’ or ‘Microsoft Excel’.

This is an abstract attribute, you must override this!

BaseExporter.export_form_fields

When the event’s administrator visits the export page, this method is called to return the configuration fields available.

It should therefore return a dictionary where the keys should be field names and the values should be corresponding Django form fields.

We suggest that you return an OrderedDict object instead of a dictionary. Your implementation could look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@property
def export_form_fields(self):
    return OrderedDict(
        [
            ('tab_width',
             forms.IntegerField(
                 label=_('Tab width'),
                 default=4
             ))
        ]
    )
BaseExporter.render(form_data: dict) → typing.Tuple[[str, str], bytes]

Render the exported file and return a tuple consisting of a filename, a file type and file content.

Parameters:form_data (dict) – The form data of the export details form

Note: If you use a ModelChoiceField (or a ModelMultipleChoiceField), the form_data will not contain the model instance but only it’s primary key (or a list of primary keys) for reasons of internal serialization when using background tasks.

This is an abstract method, you must override this!