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

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