Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 behaviour
  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 behaviour 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 guides
  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 package yourappsfolder/order
  37. Creating admin.py
  38. Creating app config
  39. Creating models.py
  40. Creating migrations folder
  41. Replace the entry 'oscar.apps.order.apps.OrderConfig' with 'yourappsfolder.order.apps.OrderConfig' in INSTALLED_APPS
  42. ``oscar_fork_app`` has an optional third argument, which allows specifying
  43. the sub-package name of the new app. For example, calling
  44. ``./manage.py oscar_fork_app order yourproject/ yoursubpackage.order`` places
  45. the ``order`` app in the
  46. ``project_root/yourproject/yoursubpackage/order`` directory.
  47. Replace Oscar's app with your own in ``INSTALLED_APPS``
  48. =======================================================
  49. You will need to let Django know that you replaced one of Oscar's core
  50. apps. You can do that by replacing its entry in the ``INSTALLED_APPS`` setting,
  51. with that for your own app.
  52. .. note::
  53. Overrides of dashboard applications should follow overrides of core
  54. applications (basket, catalogue etc), since they depend on models,
  55. declared in the core applications. Otherwise, it could cause issues
  56. with Oscar's dynamic model loading.
  57. If you want to customise one of the dashboard applications, for instance
  58. ``yourappsfolder.dashboard.catalogue``, you also need to fork the core
  59. dashboard application ``yourappsfolder.dashboard``.
  60. Example:
  61. .. code:: django
  62. INSTALLED_APPS = [
  63. # all your non-Oscar apps
  64. ...
  65. # core applications
  66. 'yourappsfolder.catalogue.apps.CatalogueConfig',
  67. 'yourappsfolder.order.apps.OrderConfig',
  68. # dashboard applications
  69. 'yourappsfolder.dashboard.apps.DashboardConfig',
  70. 'yourappsfolder.dashboard.orders.apps.OrdersDashboardConfig',
  71. 'yourappsfolder.dashboard.reports.apps.ReportsDashboardConfig',
  72. ]
  73. Start customising!
  74. ==================
  75. You can now override every class (that is
  76. :doc:`dynamically loaded </topics/class_loading_explained>`, which is
  77. almost every class) in the app you've replaced. That means forms,
  78. views, strategies, etc. All you usually need to do is give it the same name
  79. and place it in a module with the same name.
  80. Suppose you want to alter the way order numbers are generated. By default,
  81. the class ``oscar.apps.order.utils.OrderNumberGenerator`` is used. So just
  82. create a class within your ``order`` app which
  83. matches the module path from oscar: ``order.utils.OrderNumberGenerator``. This
  84. could subclass the class from Oscar or not::
  85. # yourproject/order/utils.py
  86. from oscar.apps.order.utils import OrderNumberGenerator as CoreOrderNumberGenerator
  87. class OrderNumberGenerator(CoreOrderNumberGenerator):
  88. def order_number(self, basket=None):
  89. num = super().order_number(basket)
  90. return "SHOP-%s" % num
  91. To obtain an Oscar app's app config instance, look it up in the Django app
  92. registry.