I'm trying to add a Saleor plugin that is also a Django app. The reason is that I want to be able to use Django migrations for it. I've created regular Saleor plugins before, which work fine. I'm not very familiar with Django apps yet, but the documentation makes sense to me. What I'm utterly confused about is the combination of the two concepts.
Which directory does it go into? Does it go into the saleor/plugins directory like all other regular Saleor plugins? Or directly into the saleor directory, like all other Django apps? The only somewhat related answer I could find suggests using
manage.py startapp
, which creates the plugin in the root directory, next to the saleor directory, adding to my confusion.How to install the Django app as a Saleor plugin? The official documentation instructs to use a
setup.py
and suggests that:If your plugin is a Django app, the package name (the part before the equal sign) will be added to Django's INSTALLED_APPS so you can take advantage of Django's features such as ORM integration and database migrations.
However, none of the built-in Saleor plugins or Django apps are using this setup.py mechanism, and I cannot find any Saleor-related example using this, and haven't been able to come up with anything coherent without such an example (also due to the previous point). The documentation appears to suggest that, when using setup.py, the Django app would automatically be added to Django's
INSTALLED_APPS
. Or do I have to add it there myself?
Does anyone know of an example of how to create a Saleor plugin as a Django app, preferably with working migrations?
I'm not sure that this is the best way to do it, but this works for me, for now:
The app/plugin can live anywhere, but I think it makes sense to put it into
saleor/plugins
.Create a folder for the plugin, e.g.
saleor/plugins/product_import
Run
python3 manage.py startapp product_import saleor/plugins/product_import
. This creates the standard Django app template in your folder.I tried the
setup.py
part from the official docs, but it didn't appear to do anything for me, so I finally ignored it and manually added the app tosaleor/settings.py
:This enables all the usual Django app features, such as migrations (run
python3 manage.py makemigrations product_import
after defining some models inmodels.py
), commands, etc.To turn the Django app into a Saleor plugin (to be able to use all the Saleor plugin hooks), create a
plugin.py
like this one:And add it to the list of Saleor plugins in
saleor/settings.py
: