| | 87 | Other workarounds |
| | 88 | ================= |
| | 89 | |
| | 90 | Load the JSON1 extension dynamically |
| | 91 | ------------------------------------ |
| | 92 | |
| | 93 | The following workaround works by compiling the JSON1 extension as a loadable extension |
| | 94 | and loading it when the database connection is created by utilizing Django's |connection_created|__ |
| | 95 | signal. It hasn't been tested, but it should work on Linux, macOS, and Windows. |
| | 96 | |
| | 97 | .. |connection_created| replace:: ``connection_created`` |
| | 98 | .. __: https://docs.djangoproject.com/en/3.1/ref/signals/#connection-created |
| | 99 | |
| | 100 | - Download the `SQLite amalgamation`_, with or without the configuration script. |
| | 101 | - Extract the source code archive and enter the directory of the result. |
| | 102 | - Follow the instructions to `compile a loadable extension`__ according to your operating system, |
| | 103 | but replace the ``YourCode.c`` placeholder to point to ``ext/misc/json1.c``. |
| | 104 | |
| | 105 | For example, on Linux it would be:: |
| | 106 | |
| | 107 | gcc -g -fPIC -shared ext/misc/json1.c -o json1.so |
| | 108 | |
| | 109 | On macOS, it would be:: |
| | 110 | |
| | 111 | gcc -g -fPIC -dynamiclib ext/misc/json1.c -o json1.dylib |
| | 112 | |
| | 113 | On Windows with MSVC, it would be:: |
| | 114 | |
| | 115 | cl ext/misc/json1.c -link -dll -out:json1.dll |
| | 116 | |
| | 117 | On Windows with MinGW, it would be:: |
| | 118 | |
| | 119 | gcc -g -shared ext/misc/json1.c -o json1.dll |
| | 120 | |
| | 121 | .. __: https://www.sqlite.org/loadext.html#compiling_a_loadable_extension |
| | 122 | |
| | 123 | - Place the compiled JSON1 extension somewhere desirable. |
| | 124 | - Create a signal handler for the ``connection_created`` signal. For example: |
| | 125 | |
| | 126 | .. code-block:: python |
| | 127 | |
| | 128 | def load_json1(connection, **kwargs): |
| | 129 | if connection.vendor != 'sqlite': |
| | 130 | return |
| | 131 | connection.connection.enable_load_extension(True) |
| | 132 | connection.connection.load_extension('./json1') |
| | 133 | |
| | 134 | You should replace ``'./json1'`` if your compiled extension is stored under a different directory. |
| | 135 | |
| | 136 | - Connect the receiver function in one of your apps' ``ready()`` function. |
| | 137 | |
| | 138 | .. code-block:: python |
| | 139 | |
| | 140 | class MyAppConfig(AppConfig): |
| | 141 | # ... |
| | 142 | |
| | 143 | def ready(self): |
| | 144 | connection_created.connect(load_json1) |
| | 145 | |
| | 146 | You can also connect the function using the ``@receiver`` decorator. For more information, |
| | 147 | read the docs on how to `connect receiver functions`__. |
| | 148 | |
| | 149 | .. __: https://docs.djangoproject.com/en/3.1/topics/signals/#connecting-receiver-functions |