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.

how_to_customise_models.rst 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. =======================
  2. How to customise models
  3. =======================
  4. This How-to describes how to replace Oscar models with your own. This allows you
  5. to add fields and custom methods. It builds upon the steps described in
  6. :doc:`/topics/customisation`. Please read it first and ensure that you've:
  7. * Created a Python module with the the same app label
  8. * Added it as Django app to ``INSTALLED_APPS``
  9. * Added a ``models.py`` and ``admin.py``
  10. Example
  11. -------
  12. Suppose you want to add a ``video_url`` field to the core product model. This means
  13. that you want your application to use a subclass of
  14. :class:`oscar.apps.catalogue.abstract_models.AbstractProduct` which has an additional field.
  15. The first step is to create a local version of the "catalogue" app. At a minimum, this
  16. involves creating ``catalogue/models.py`` within your project and changing ``INSTALLED_APPS``
  17. to point to your local version rather than Oscar's.
  18. Next, you can modify the ``Product`` model through subclassing::
  19. # yourproject/catalogue/models.py
  20. from django.db import models
  21. from oscar.apps.catalogue.abstract_models import AbstractProduct
  22. class Product(AbstractProduct):
  23. video_url = models.URLField()
  24. from oscar.apps.catalogue.models import *
  25. Make sure to import the remaining Oscar models at the bottom of your file.
  26. .. tip::
  27. Using ``from ... import *`` is strange isn't it? Yes it is, but it needs to
  28. be done at the bottom of the module due to the way Django registers models.
  29. The order that model classes are imported makes a difference, with only the
  30. first one for a given class name being registered.
  31. The last thing you need to do now is make Django update the database schema and
  32. create a new column in the product table. We recommend using South migrations
  33. for this (internally Oscar already does this) so all you need to do is create a
  34. new schema migration.
  35. It is possible to simply create a new catalogue migration (using ``./manage.py
  36. schemamigration catalogue --auto``) but this isn't recommended as any
  37. dependencies between migrations will need to be applied manually (by adding a
  38. ``depends_on`` attribute to the migration class).
  39. The recommended way to handle migrations is to copy the ``migrations`` directory
  40. from ``oscar/apps/catalogue`` into your new ``catalogue`` app. Then you can
  41. create a new (additional) schemamigration using the ``schemamigration``
  42. management command::
  43. ./manage.py schemamigration catalogue --auto
  44. which will pick up any customisations to the product model.
  45. To apply the migration you just created, all you have to do is run
  46. ``./manage.py migrate catalogue`` and the new column is added to the product
  47. table in the database.
  48. Customising Products
  49. --------------------
  50. You should inherit from ``AbstractProduct`` as above to alter behaviour for all
  51. your products. Further subclassing is not recommended, because using methods
  52. and attributes of concrete subclasses of ``Product`` are not available unless
  53. explicitly casted to that class.
  54. To model different classes of products, use ``ProductClass`` and
  55. ``ProductAttribute`` instead.
  56. Model customisations are not picked up
  57. --------------------------------------
  58. It's a common problem that you're trying to customise one of Oscar's models,
  59. but your new fields don't seem to get picked up. That is usually caused by
  60. Oscar's models being imported before your customised ones. Django's model
  61. registration disregards all further model declarations.
  62. In your overriding ``models.py``, ensure that you import Oscar's models *after*
  63. your custom ones have been defined. If that doesn't help, you have an import
  64. from ``oscar.apps.*.models`` somewhere that is being executed before your models
  65. are parsed. One trick for finding that import: put ``assert False`` in the relevant
  66. Oscar's models.py, and the stack trace will show you the importing module.
  67. If other modules need to import your models, then import from your local module,
  68. not from Oscar directly.