Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

customisation.rst 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. =================
  2. Customising Oscar
  3. =================
  4. Many parts of Oscar can be adapted to your needs like any other Django
  5. application:
  6. * Many :doc:`settings</ref/settings>` control Oscar's behavior
  7. * The looks can be controlled by extending or overriding the
  8. :doc:`templates </howto/how_to_customise_templates>`
  9. But as Oscar is built as a highly customisable and extendable framework, it
  10. doesn't stop there. The behaviour of all Oscar apps can heavily be altered
  11. by injecting your own code.
  12. To extend the behavior of an Oscar core app, it needs to be forked, which is
  13. achieved with a simple management command. Afterwards, you should
  14. generally be able to override any class/model/view by just dropping it
  15. in the right place and giving it the same name.
  16. In some cases, customising is slightly more involved. The following how-tos
  17. give plenty of examples for specific use cases:
  18. * :doc:`/howto/how_to_customise_models`
  19. * :doc:`/howto/how_to_change_a_url`
  20. * :doc:`/howto/how_to_customise_a_view`
  21. For a deeper understanding of customising Oscar, the following documents are
  22. recommended:
  23. * :doc:`/internals/design-decisions`
  24. * :doc:`Dynamic class loading</topics/class_loading_explained>`
  25. * :doc:`fork_app`
  26. .. _fork-oscar-app:
  27. Fork the Oscar app
  28. ==================
  29. If this is the first time you're forking an Oscar app, you'll need to create
  30. a root module under which all your forked apps will live::
  31. $ mkdir yourappsfolder
  32. $ touch yourappsfolder/__init__.py
  33. Now you call the helper management command which creates some basic files for
  34. you. It is explained in detail in :doc:`fork_app`. Run it like this::
  35. $ ./manage.py oscar_fork_app order yourappsfolder/
  36. Creating folder apps/order
  37. Creating __init__.py and admin.py
  38. Creating models.py and copying migrations from [...] to [...]
  39. .. note::
  40. For forking app in project root directory, call ``oscar_fork_app`` with ``.`` (dot) instead of ``yourproject/`` path.
  41. Example:
  42. Calling ``./manage.py oscar_fork_app order yourproject/`` ``order`` app will be placed in ``project_root/yourproject/`` directory.
  43. Calling ``./manage.py oscar_fork_app order .`` ``order`` app will be placed in ``project_root/`` directory.
  44. Replace Oscar's app with your own in ``INSTALLED_APPS``
  45. =======================================================
  46. You will need to let Django know that you replaced one of Oscar's core
  47. apps. You can do that by supplying an extra argument to
  48. ``get_core_apps`` function::
  49. # settings.py
  50. from oscar import get_core_apps
  51. # ...
  52. INSTALLED_APPS = [
  53. # all your non-Oscar apps
  54. ] + get_core_apps(['yourappsfolder.order'])
  55. ``get_core_apps([])`` will return a list of Oscar core apps. If you supply a
  56. list of additional apps, they will be used to replace the Oscar core apps.
  57. In the above example, ``yourproject.order`` will be returned instead of
  58. ``oscar.apps.order``.
  59. .. note::
  60. Overrides of dashboard applications should follow overrides of core
  61. applications (basket, catalogue etc), since they depend on models,
  62. declared in the core applications. Otherwise, it could cause issues
  63. with Oscar's dynamic model loading.
  64. Example:
  65. .. code:: django
  66. INSTALLED_APPS = [
  67. # all your non-Oscar apps
  68. ] + get_core_apps([
  69. # core applications
  70. 'yourappsfolder.catalogue',
  71. 'yourappsfolder.order',
  72. # dashboard applications
  73. 'yourappsfolder.dashboard',
  74. 'yourappsfolder.dashboard.orders',
  75. 'yourappsfolder.dashboard.reports',
  76. ])
  77. Start customising!
  78. ==================
  79. You can now override every class (that is
  80. :doc:`dynamically loaded </topics/class_loading_explained>`, which is
  81. almost every class) in the app you've replaced. That means forms,
  82. views, strategies, etc. All you usually need to do is give it the same name
  83. and place it in a module with the same name.
  84. Suppose you want to alter the way order numbers are generated. By default,
  85. the class ``oscar.apps.order.utils.OrderNumberGenerator`` is used. So just
  86. create a class within your ``order`` app which
  87. matches the module path from oscar: ``order.utils.OrderNumberGenerator``. This
  88. could subclass the class from Oscar or not::
  89. # yourproject/order/utils.py
  90. from oscar.apps.order.utils import OrderNumberGenerator as CoreOrderNumberGenerator
  91. class OrderNumberGenerator(CoreOrderNumberGenerator):
  92. def order_number(self, basket=None):
  93. num = super().order_number(basket)
  94. return "SHOP-%s" % num