Skip to content

Commit

Permalink
Correct the documentation of how to import extension snippets
Browse files Browse the repository at this point in the history
The method documented here (adding to PROJECT/schema.py and PROJECT/__init__.py) is too early, before the app is ready. Instead copied the instructions at https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-5-extensions which loads the extensions in the ready() method
  • Loading branch information
n2ygk authored Jul 30, 2024
1 parent cc91637 commit ec9dc1e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions docs/blueprints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ Blueprint files can be found `here <https://github.com/tfranzel/drf-spectacular/

.. note:: Simply copy&paste the snippets into your codebase. The extensions register
themselves automatically. Just be sure that the python interpreter sees them at least once.
To that end, we suggest creating a ``PROJECT/schema.py`` file and importing it in your
``PROJECT/__init__.py`` (same directory as ``settings.py`` and ``urls.py``)
with ``import PROJECT.schema``. Now you are all set.
It is good practice to collect your extensions in ``YOUR_MAIN_APP_NAME/schema.py`` and importing that
file in your ``YOUR_MAIN_APP_NAME/apps.py``. Every proper Django app will already have an auto-generated
``apps.py`` file. Although not strictly necessary, doing the import in ``ready()`` is the most robust
approach. It will make sure your environment (e.g. settings) is properly set up prior to loading.

.. code-block:: python
# your_main_app_name/apps.py
class YourMainAppNameConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "your_main_app_name"
def ready(self):
import your_main_app_name.schema # noqa: E402
dj-stripe
---------
Expand Down

0 comments on commit ec9dc1e

Please sign in to comment.