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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 an Oscar core app, you will at least need to create an
  14. app with the same label, and replace it in ``INSTALLED_APPS``.
  15. Depending on what should be adapted, different steps are necessary beyond that.
  16. The steps are detailed below; this overview might help you to figure out what
  17. needs to be done.
  18. ======================================= ============================= ================= =================
  19. Goals vs. necessary steps Python module with same label Add as Django app Custom ``app.py``
  20. ======================================= ============================= ================= =================
  21. Override model, view or any other class Necessary Necessary Not necessary
  22. Change app URLs or add views Necessary Necessary Necessary
  23. ======================================= ============================= ================= =================
  24. Please also refer to the following how-tos for further instructions and examples.
  25. * :doc:`/howto/how_to_customise_models`
  26. * :doc:`/howto/how_to_change_a_url`
  27. * :doc:`/howto/how_to_customise_a_view`
  28. * :doc:`/howto/how_to_override_a_core_class`
  29. * :doc:`/howto/how_to_customise_a_mixin`
  30. Python module with same label
  31. =============================
  32. All advanced customisation requires creating an a Python module with the same
  33. "app label" as the Oscar app you want to extend.
  34. E.g., to create a local version of ``oscar.apps.order``, do the following::
  35. $ mkdir yourproject/order
  36. $ touch yourproject/order/__init__.py
  37. Add as Django app
  38. =================
  39. You will need to let Django know that you intend to replace one of Oscar's core
  40. apps. This means overriding it in ``INSTALLED_APPS`` and creating a few hooks
  41. back to the replaced Oscar app.
  42. ``INSTALLED_APPS`` override
  43. ---------------------------
  44. You will need to replace Oscar's version of the app with yours in
  45. ``INSTALLED_APPS`` . You can do that by supplying an extra argument to
  46. ``get_core_apps`` function::
  47. # settings.py
  48. from oscar import get_core_apps
  49. # ...
  50. INSTALLED_APPS = [
  51. # all your non-Oscar apps
  52. ] + get_core_apps(['yourproject.order'])
  53. ``get_core_apps([])`` will return a list of Oscar core apps. If you supply a
  54. list of additional apps, they will be used to replace the Oscar core apps.
  55. In the above example, ``yourproject.order`` will be returned instead of
  56. ``oscar.apps.order``.
  57. To get your app working, you might also need to create a custom ``models.py``
  58. and ``admin.py``.
  59. models.py
  60. ---------
  61. If the original Oscar app has a ``models.py``, you'll need to create a
  62. ``models.py`` file in your local app. It should import all models from
  63. the oscar app being overridden::
  64. # yourproject/order/models.py
  65. # your custom models go here
  66. from oscar.apps.order.models import *
  67. If two models with the same name are declared within an app, Django will only
  68. use the first one. That means that if you wish to customise Oscar's models, you
  69. must declare your custom ones before importing Oscar's models for that app.
  70. If you're using South, you have to copy the ``migrations`` directory
  71. from ``oscar/apps/order`` and put it into your ``order`` app. Detailed
  72. instructions are available in :doc:`/howto/how_to_customise_models`.
  73. admin.py
  74. --------
  75. When you replace one of Oscar's apps with a local one, Django admin integration
  76. is lost. If you'd like to use it, you need to create an ``admin.py`` and import
  77. the core app's ``admin.py`` (which will run the register code)::
  78. # yourproject/order/admin.py
  79. import oscar.apps.order.admin
  80. This isn't great but we haven't found a better way as of yet.
  81. Custom ``app.py``
  82. =================
  83. Oscar's views and URLs use a tree of 'app' instances, each of which subclass
  84. :class:`oscar.core.application.Application` and provide ``urls`` property.
  85. Oscar has a root app instance in ``oscar/app.py`` which should already be
  86. wired up in your ``urls.py``::
  87. # urls.py
  88. from oscar.app import application
  89. urlpatterns = [
  90. ... # Your other URLs
  91. url(r'', include(application.urls)),
  92. ]
  93. Modifying root app
  94. ------------------
  95. If you want to change URLs or views of the root application above, you need to
  96. replace it with your own ``application`` instance, that (usually) subclasses
  97. Oscar's. Hence, create ``yourproject/app.py`` with contents::
  98. # yourproject/app.py
  99. from oscar.app import Shop
  100. class BaseApplication(Shop):
  101. pass
  102. application = BaseApplication()
  103. Now hook this up in your ``urls.py`` instead::
  104. # urls.py
  105. from yourproject.app import application
  106. urlpatterns = [
  107. ...
  108. url(r'', include(application.urls)),
  109. ]
  110. Modifying sub-apps
  111. ------------------
  112. Sub-apps such as the ``catalogue`` app are loaded dynamically, just as most
  113. other classes in Oscar::
  114. # oscar/app.py
  115. class Shop(Application):
  116. name = None
  117. catalogue_app = get_class('catalogue.app', 'application')
  118. customer_app = get_class('customer.app', 'application')
  119. ...
  120. That means you can leave the root app unchanged, and just need to create another
  121. ``application`` instance. It will usually inherit from Oscar's version::
  122. # yourproject/promotions/app.py
  123. from oscar.apps.promotions.app import PromotionsApplication as CorePromotionsApplication
  124. from .views import MyExtraView
  125. class PromotionsApplication(CorePromotionsApplication):
  126. extra_view = MyExtraView
  127. application = PromotionsApplication()