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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 Override model class Override view class (or change URLs) Override any other class
  19. ================================== ==================== ==================================== ========================
  20. Python module with same label Necessary Necessary Necessary
  21. Custom root and local ``app.py`` Not necessary Necessary Not necessary
  22. Add as Django app Necessary Not necessary Necessary
  23. ================================== ==================== ==================================== ========================
  24. If more complex changes are desired, it is usually easiest to do all of the
  25. steps.
  26. Please also refer to the following how-tos for further instructions and examples.
  27. * :doc:`/howto/how_to_customise_models`
  28. * :doc:`/howto/how_to_change_a_url`
  29. * :doc:`/howto/how_to_customise_a_view`
  30. * :doc:`/howto/how_to_override_a_core_class`
  31. Python module with same label
  32. =============================
  33. All advanced customisation requires creating an a Python module with the same
  34. "app label" as the Oscar app you want to extend.
  35. E.g., to create a local version of ``oscar.apps.order``, do the following::
  36. $ mkdir yourproject/order
  37. $ touch yourproject/order/__init__.py
  38. Custom root and local ``app.py``
  39. ================================
  40. Root ``app.py``
  41. ---------------
  42. Oscar's views and URLs use a tree of 'app' instances, each of which subclass
  43. :class:`oscar.core.application.Application` and provide ``urls`` property.
  44. Oscar has a root app instance in ``oscar/app.py`` which can be imported into
  45. your ``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`` instead::
  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 added ``BaseApplication``.
  69. Local ``app.py``
  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 root ``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. Add as Django app
  85. =================
  86. You will need to let Django know that you intend to replace one of Oscar's core
  87. apps. This means overriding it in ``INSTALLED_APPS`` and creating a few hooks
  88. back to the replaced Oscar app.
  89. ``INSTALLED_APPS`` override
  90. ---------------------------
  91. You will need to replace Oscar's version of the app with yours in
  92. ``INSTALLED_APPS`` . You can do that by supplying an extra argument to
  93. ``get_core_apps`` function::
  94. # settings.py
  95. from oscar import get_core_apps
  96. # ...
  97. INSTALLED_APPS = [
  98. # all your non-Oscar apps
  99. ] + get_core_apps(['yourproject.order'])
  100. ``get_core_apps([])`` will return a list of Oscar core apps. If you supply a
  101. list of additional apps, they will be used to replace the Oscar core apps.
  102. In the above example, ``yourproject.order`` will be returned instead of
  103. ``oscar.apps.order``.
  104. To get your app working, you might also need to create a custom ``models.py``
  105. and ``admin.py``.
  106. models.py
  107. ---------
  108. If the original Oscar app has a ``models.py``, you'll need to create a
  109. ``models.py`` file in your local app. It should import all models from
  110. the oscar app being overridden::
  111. # yourproject/order/models.py
  112. # your custom models go here
  113. from oscar.apps.order.models import *
  114. If two models with the same name are declared within an app, Django will only
  115. use the first one. That means that if you wish to customise Oscar's models, you
  116. must declare your custom ones before importing Oscar's models for that app.
  117. If you're using South, you probably have to copy the ``migrations`` directory
  118. from ``oscar/apps/order`` and put it into your ``order`` app. Detailed
  119. instructions are available in :doc:`/howto/how_to_customise_models`.
  120. admin.py
  121. --------
  122. When you replace one of Oscar's apps with a local one, Django admin integration
  123. is lost. If you'd like to use it, you need to create an ``admin.py`` and import
  124. the core app's ``admin.py`` (which will run the register code)::
  125. # yourproject/order/admin.py
  126. import oscar.apps.order.admin
  127. This isn't great but we haven't found a better way as of yet.