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

customisation.rst 3.9KB

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