skip to Main Content

A Django custom command to write model fields on an Excel file

Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code.

Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need.

You can use the following Django custom command:

You can run the command like this:

./manage.py excel_models app1 app2 models.xlsx

And it will write all the models of app1 and app2 on the models.xlsx file. Every model will be written on a different sheet.

The option –exclude-models can be used to specify a text file with models to exclude from Excel file (one per line).

The option –include-reverse-relations (default false) can be used to include also reverse relations of your models, that are fields that are not part of the models itself, but are reverse fields from other models that have for instance a ForeignKey to the model.

The command depends on the XlsxWriter package to create the Excel file.

augusto

Freelance developer and sysadmin

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top