How can I create a Saleor plugin that is a Django app?

1.6k Views Asked by At

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?

1

There are 1 best solutions below

3
On

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.

  1. Create a folder for the plugin, e.g. saleor/plugins/product_import

  2. Run python3 manage.py startapp product_import saleor/plugins/product_import. This creates the standard Django app template in your folder.

  3. 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 to saleor/settings.py:

    INSTALLED_APPS = [
      ...
      "saleor.plugins.product_import",
      ...
    ]
    

    This enables all the usual Django app features, such as migrations (run python3 manage.py makemigrations product_import after defining some models in models.py), commands, etc.

  4. 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:

    from django.http import JsonResponse
    from django.core.handlers.wsgi import WSGIRequest
    
    from ..base_plugin import BasePlugin
    
    class ProductImportPlugin(BasePlugin):
      """Imports products into Saleor"""
    
      PLUGIN_NAME = "Product Import Plugin"
      PLUGIN_ID = "product_import"
      DEFAULT_ACTIVE = True
      PLUGIN_DESCRIPTION = (
        "Imports products into Saleor."
      )
    
      def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    
      def webhook(self, request: WSGIRequest, path: str, previous_value) -> JsonResponse:
        return JsonResponse({"status": "works"})
    

    And add it to the list of Saleor plugins in saleor/settings.py:

    PLUGINS = [
      ...,
      "saleor.plugins.product_import.plugin.ProductImportPlugin",
    ]