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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, it needs to be forked, which is
  13. achieved with a simple management command. Afterwards, 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, the following documents are
  22. recommended:
  23. * :doc:`/internals/design-decisions`
  24. * :doc:`Dynamic class loading</topics/class_loading_explained>`
  25. * :doc:`fork_app`
  26. Fork the Oscar app
  27. ==================
  28. If this is the first time you're forking an Oscar app, you'll need to create
  29. a root module under which all your forked apps will live::
  30. $ mkdir yourproject
  31. $ touch yourproject/__init__.py
  32. Now you call the helper management command which creates some basic files for
  33. you. It is explained in detail in :doc:`fork_app`. Run it like this::
  34. $ ./manage.py oscar_fork_app order yourproject/
  35. Creating folder apps/order
  36. Creating __init__.py and admin.py
  37. Creating models.py and copying migrations from [...] to [...]
  38. Replace Oscar's app with your own in ``INSTALLED_APPS``
  39. =======================================================
  40. You will need to let Django know that you replaced one of Oscar's core
  41. apps. You can do that by supplying an extra argument to
  42. ``get_core_apps`` function::
  43. # settings.py
  44. from oscar import get_core_apps
  45. # ...
  46. INSTALLED_APPS = [
  47. # all your non-Oscar apps
  48. ] + get_core_apps(['yourproject.order'])
  49. ``get_core_apps([])`` will return a list of Oscar core apps. If you supply a
  50. list of additional apps, they will be used to replace the Oscar core apps.
  51. In the above example, ``yourproject.order`` will be returned instead of
  52. ``oscar.apps.order``.
  53. Start customising!
  54. ==================
  55. You can now override every class (that is
  56. :doc:`dynamically loaded </topics/class_loading_explained>`, which is
  57. almost every class) in the app you've replaced. That means forms,
  58. views, strategies, etc. All you usually need to do is give it the same name
  59. and place it in a module with the same name.
  60. Suppose you want to alter the way order numbers are generated. By default,
  61. the class ``oscar.apps.order.utils.OrderNumberGenerator`` is used. So just
  62. create a class within your ``order`` app which
  63. matches the module path from oscar: ``order.utils.OrderNumberGenerator``. This
  64. could subclass the class from Oscar or not::
  65. # yourproject/order/utils.py
  66. from oscar.apps.order.utils import OrderNumberGenerator as CoreOrderNumberGenerator
  67. class OrderNumberGenerator(CoreOrderNumberGenerator):
  68. def order_number(self, basket=None):
  69. num = super(OrderNumberGenerator, self).order_number(basket)
  70. return "SHOP-%s" % num