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.

customisation.rst 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. Almost every aspect of it can be altered.
  11. :doc:`Various techniques </internals/design-decisions>` are employed to achieve
  12. that level of adaptability.
  13. To extend the behavior of a Oscar core app, you will at least need to create an
  14. app with the same label. Depending on what should be adapted, different steps
  15. are necessary beyond that. The steps are detailed below; this overview might
  16. help you to figure out what needs to be done.
  17. ================================== ============= ============= ============= ==================
  18. Goals vs. necessary steps Change model Change URL Change View Replace core class
  19. ================================== ============= ============= ============= ==================
  20. Create app with same label Necessary Necessary Necessary Necessary
  21. Custom Shop class Not necessary Necessary Necessary Not necessary
  22. Custom Application class Not necessary Necessary Necessary Not necessary
  23. Override app in ``INSTALLED_APPS`` Necessary Not necessary Not necessary Necessary
  24. ``models.py`` & ``admin.py`` Necessary Not necessary Not necessary Not necessary
  25. ================================== ============= ============= ============= ==================
  26. If more complex changes are desired, it is usually easiest to do all of the
  27. steps.
  28. Please also refer to the following how-tos for further instructions and examples.
  29. * :doc:`/howto/how_to_customise_models`
  30. * :doc:`/howto/how_to_change_a_url`
  31. * :doc:`/howto/how_to_customise_a_view`
  32. * :doc:`/howto/how_to_override_a_core_class`
  33. Create app with same label
  34. --------------------------
  35. All advanced customisation requires creating an a Python package with the same
  36. "app label" as the Oscar app you want to extend.
  37. E.g., to create a local version of ``oscar.apps.order``, do the following::
  38. $ mkdir yourproject/order
  39. $ touch yourproject/order/__init__.py
  40. Custom Shop class
  41. -----------------
  42. Oscar's views and URLs use a tree of 'app' instances, each of which subclass
  43. ``oscar.core.application.Application`` and provide ``urls`` property. Oscar has
  44. a root app instance in ``oscar/app.py`` which can be imported into your
  45. ``urls.py``::
  46. # urls.py
  47. from oscar.app import application
  48. urlpatterns = patterns('',
  49. ... # Your other URLs
  50. (r'', include(application.urls)),
  51. )
  52. To get control over the mapping between URLs and views, you need to use a local
  53. ``application`` instance, that (usually) subclasses Oscar's. Hence, create
  54. ``yourproject/app.py`` with contents::
  55. # yourproject/app.py
  56. from oscar.app import Shop
  57. class BaseApplication(Shop):
  58. pass
  59. application = BaseApplication()
  60. Now hook this up in your ``urls.py``::
  61. # urls.py
  62. from yourproject.app import application
  63. urlpatterns = patterns('',
  64. ...
  65. (r'', include(application.urls)),
  66. )
  67. This step only needs to be done once. All customisation will only entail
  68. overriding parts of the newly created ``BaseApplication``.
  69. Custom application class
  70. ------------------------
  71. If you want to modify a view or change a URL, you need to create an ``app.py``
  72. for your local app. It will usually inherit from Oscar's version::
  73. # yourproject/order/app.py
  74. from oscar.apps.promotions.app import PromotionsApplication as CorePromotionsApplication
  75. class PromotionsApplication(CorePromotionsApplication):
  76. pass
  77. application = PromotionsApplication()
  78. and hook it up in your main ``app.py``::
  79. # yourproject/app.py
  80. from oscar.app import Shop
  81. from yourproject.promotions.app import application as promotions_app
  82. class BaseApplication(Shop):
  83. promotions_app = promotions_app
  84. Override app in INSTALLED_APPS
  85. ------------------------------
  86. You will need to add your app ``INSTALLED_APPS`` to override Oscar's version
  87. of the app with yours. You can do that by supplying an extra argument to
  88. ``get_core_apps`` function::
  89. # settings.py
  90. from oscar import get_core_apps
  91. # ...
  92. INSTALLED_APPS = [
  93. # all your non-Oscar apps
  94. ] + get_core_apps(['yourproject.order'])
  95. ``get_core_apps([])`` will return a list of Oscar core apps. If you supply a
  96. list of additional apps, they will be used to replace the Oscar core apps.
  97. In the above example, ``yourproject.order`` will be returned instead of
  98. ``oscar.apps.order``.
  99. models.py & admin.py
  100. --------------------
  101. Create a ``models.py`` file in your local app. It should import all models from
  102. the oscar app being overridden::
  103. # yourproject/order/models.py
  104. # your custom models go here
  105. from oscar.apps.order.models import *
  106. One pain point with replacing one of Oscar's apps with a local one in
  107. ``INSTALLED_APPS`` is that admin integration is lost from the original
  108. app. If you'd like to use the Django admin functionality you just need
  109. to create an ``admin.py`` and import the core app's ``admin.py`` (which will
  110. run the register code)::
  111. # yourproject/order/admin.py
  112. import oscar.apps.order.admin
  113. This isn't great but we haven't found a better way as of yet.