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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. The behaviour of all Oscar apps can heavily be altered
  11. by injecting your own code.
  12. To extend the behavior of an Oscar core app, some bootstrapping is required.
  13. These steps are detailed below. After having completed this, you should
  14. generally be able to override any class/model/view by just dropping it
  15. in the right place and giving it the same name.
  16. In some cases, customising is slightly more involved. The following how-tos
  17. give plenty of examples for specific use cases:
  18. * :doc:`/howto/how_to_customise_models`
  19. * :doc:`/howto/how_to_change_a_url`
  20. * :doc:`/howto/how_to_customise_a_view`
  21. For a deeper understanding of customising Oscar, it is recommended to read
  22. through the :doc:`/internals/design-decisions` and the concept of
  23. :doc:`dynamic class loading</topics/class_loading_explained>`, as it underpins
  24. most of what is detailed below.
  25. Create Python module with same label
  26. ====================================
  27. You need to create a Python module with the same "app label" as the Oscar app
  28. you want to extend.
  29. E.g., to create a local version of ``oscar.apps.order``, do the following::
  30. $ mkdir yourproject/order
  31. $ touch yourproject/order/__init__.py
  32. Replace Oscar's app with your own in ``INSTALLED_APPS``
  33. =======================================================
  34. You will need to let Django know that you intend to replace one of Oscar's core
  35. apps. You can do that by supplying an extra argument to
  36. ``get_core_apps`` function::
  37. # settings.py
  38. from oscar import get_core_apps
  39. # ...
  40. INSTALLED_APPS = [
  41. # all your non-Oscar apps
  42. ] + get_core_apps(['yourproject.order'])
  43. ``get_core_apps([])`` will return a list of Oscar core apps. If you supply a
  44. list of additional apps, they will be used to replace the Oscar core apps.
  45. In the above example, ``yourproject.order`` will be returned instead of
  46. ``oscar.apps.order``.
  47. Reference Oscar's models
  48. ========================
  49. If the original Oscar app has a ``models.py``, you'll need to create a
  50. ``models.py`` file in your local app. It should import all models from
  51. the Oscar app being overridden::
  52. # yourproject/order/models.py
  53. # your custom models go here
  54. from oscar.apps.order.models import *
  55. If two models with the same name are declared within an app, Django will only
  56. use the first one. That means that if you wish to customise Oscar's models, you
  57. must declare your custom ones before importing Oscar's models for that app.
  58. If you're using South, you have to copy the ``migrations`` directory
  59. from ``oscar/apps/order`` and put it into your ``order`` app. Detailed
  60. instructions are available in :doc:`/howto/how_to_customise_models`.
  61. Get the Django admin working
  62. ============================
  63. When you replace one of Oscar's apps with a local one, Django admin integration
  64. is lost. If you'd like to use it, you need to create an ``admin.py`` and import
  65. the core app's ``admin.py`` (which will run the register code)::
  66. # yourproject/order/admin.py
  67. import oscar.apps.order.admin
  68. This isn't great but we haven't found a better way as of yet.
  69. Start customising!
  70. ==================
  71. You can now override every class (that is
  72. :doc:`dynamically loaded </topics/class_loading_explained>`, which is
  73. almost every class) in the app you've replaced. That means forms,
  74. views, strategies, etc. All you usually need to do is give it the same name
  75. and place it in a module with the same name.
  76. Suppose you want to alter the way order numbers are generated. By default,
  77. the class ``oscar.apps.order.utils.OrderNumberGenerator`` is used. So just
  78. create a class within your ``order`` app which
  79. matches the module path from oscar: ``order.utils.OrderNumberGenerator``. This
  80. could subclass the class from Oscar or not::
  81. # yourproject/order/utils.py
  82. from oscar.apps.order.utils import OrderNumberGenerator as CoreOrderNumberGenerator
  83. class OrderNumberGenerator(CoreOrderNumberGenerator):
  84. def order_number(self, basket=None):
  85. num = super(OrderNumberGenerator, self).order_number(basket)
  86. return "SHOP-%s" % num