You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

class_loading_explained.rst 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ===============================
  2. Dynamic class loading explained
  3. ===============================
  4. Dynamic class loading is the foundation for making Oscar extensively
  5. customisable. It is hence worth understanding how it works, because most
  6. customisations depend on it.
  7. It is achieved by :meth:`oscar.core.loading.get_classes` and its
  8. single-class cousin :meth:`~oscar.core.loading.get_class`. Wherever feasible,
  9. Oscar's codebase uses ``get_classes`` instead of a regular import statement::
  10. from oscar.apps.shipping.repository import Repository
  11. is replaced by::
  12. from oscar.core.loading import get_class
  13. Repository = get_class('shipping.repository', 'Repository')
  14. .. note:: This is done for almost all classes: views, models, Application
  15. instances, etc. Every class imported by ``get_class`` can be
  16. overridden.
  17. Why?
  18. ----
  19. This structure enables a project to create a local ``shipping.repository``
  20. module, and optionally subclass the class from
  21. ``oscar.app.shipping.repository``. When Oscar tries to load the
  22. ``Repository`` class, it will load the one from your local project.
  23. This way, most classes can be overridden with minimal duplication, as only
  24. the to-be-changed classes have to be altered. They can optionally inherit from
  25. Oscar's implementation, which often amounts to little more than a few lines of
  26. custom code for changes to core behaviour.
  27. Seen on a bigger scale, this structures enables Oscar to ship with classes with
  28. minimal assumptions about the domain, and make it easy to modify behaviour as
  29. needed.
  30. How it works
  31. ------------
  32. The ``get_class`` function looks through your ``INSTALLED_APPS`` for a matching
  33. app and will attempt to load the custom class from the specified module. If the
  34. app isn't overridden or the custom module doesn't define the class, it will
  35. fall back to the default Oscar class.
  36. In practice
  37. -----------
  38. For ``get_class`` to pick up the customised class, the Oscar apps need to be
  39. forked. The process is detailed and illustrated with examples in
  40. :doc:`/topics/customisation`. It is usually enough to call ``oscar_fork_app``
  41. and replace the app in ``INSTALLED_APPS``.
  42. Using ``get_class`` in your own code
  43. ------------------------------------
  44. Generally, there is no need for ``get_class`` in your own code as the location
  45. of the module for the class is known. Some Oscar developers nonetheless
  46. use ``get_class`` when importing classes from Oscar. This means that if someday
  47. the class is overridden, it will not require code changes. Care should be taken
  48. when doing this, as this is a tricky trade-off between maintainability and
  49. added complexity.
  50. Please note that we cannot recommend ever using ``get_model`` in your own code.
  51. Model initialisation is a tricky process and it's
  52. easy to run into circular import issues.
  53. Overriding dynamic class loading behaviour
  54. ------------------------------------
  55. In some cases it may be necessary to customise the logic used by Oscar to
  56. dynamically load classes. You can do this by supplying your own class loader
  57. function to the ``OSCAR_DYNAMIC_CLASS_LOADER`` setting:
  58. OSCAR_DYNAMIC_CLASS_LOADER = 'myproject.custom_class_loader'
  59. Supply a dotted Python path to a callable that takes
  60. the same arguments as :meth:`~oscar.core.loading.default_class_loader`.
  61. Testing
  62. -------
  63. You can test whether your overriding worked by trying to get a class from your
  64. module::
  65. >>> from oscar.core.loading import get_class
  66. >>> get_class('shipping.repository', 'Repository')
  67. yourproject.shipping.repository.Repository # it worked!