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. 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 Python module with same label Add as Django app Custom ``app.py``
  19. ================================ ============================= ================= =================
  20. Override a model class Necessary Necessary Not necessary
  21. Override any other class or view 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. Custom ``app.py``
  38. =================
  39. Oscar's views and URLs use a tree of 'app' instances, each of which subclass
  40. :class:`oscar.core.application.Application` and provide ``urls`` property.
  41. Oscar has a root app instance in ``oscar/app.py`` which should already be
  42. wired up in your ``urls.py``::
  43. # urls.py
  44. from oscar.app import application
  45. urlpatterns = patterns('',
  46. ... # Your other URLs
  47. (r'', include(application.urls)),
  48. )
  49. Modifying root app
  50. ------------------
  51. If you want to change URLs or views of the root application above, you need to
  52. replace it with your own ``application`` instance, that (usually) subclasses
  53. Oscar's. Hence, create ``yourproject/app.py`` with contents::
  54. # yourproject/app.py
  55. from oscar.app import Shop
  56. class BaseApplication(Shop):
  57. pass
  58. application = BaseApplication()
  59. Now hook this up in your ``urls.py`` instead::
  60. # urls.py
  61. from yourproject.app import application
  62. urlpatterns = patterns('',
  63. ...
  64. (r'', include(application.urls)),
  65. )
  66. Modifying sub-apps
  67. ------------------
  68. Sub-apps such as the ``catalogue`` app are loaded dynamically, just as most
  69. other classes in Oscar::
  70. # oscar/app.py
  71. class Shop(Application):
  72. name = None
  73. catalogue_app = get_class('catalogue.app', 'application')
  74. customer_app = get_class('customer.app', 'application')
  75. ...
  76. That means you can leave the root app unchanged, and just need to create another
  77. ``application`` instance. It will usually inherit from Oscar's version::
  78. # yourproject/promotions/app.py
  79. from oscar.apps.promotions.app import PromotionsApplication as CorePromotionsApplication
  80. from .views import MyExtraView
  81. class PromotionsApplication(CorePromotionsApplication):
  82. extra_view = MyExtraView
  83. application = PromotionsApplication()
  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.