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

customisation.rst 6.0KB

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