Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

class_loading_explained.rst 3.4KB

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