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.


Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.