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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.
  6. It builds upon the steps described in :doc:`/topics/customisation`. Please
  7. read it first and ensure that you've:
  8. * Created a Python module with the the same label
  9. * Added it as Django app
  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.models.Product` 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. The last thing you need to do now is make Django update the database schema and
  27. create a new column in the product table. We recommend to use South migrations
  28. for this (internally Oscar already uses it) so all you need to do is create a
  29. new schema migration. Depending on your setup you should follow one of these
  30. two options:
  31. 1. You **have not** run ``./manage.py migrate`` before
  32. You can simply generate a new initial migration using::
  33. ./manage.py schemamigration catalogue --initial
  34. 2. You **have** run ``./manage.py migrate`` before
  35. You have to copy the ``migrations`` directory from ``oscar/apps/catalogue``
  36. (the same as the ``models.py`` you just copied) and put it into your
  37. ``catalogue`` app.
  38. Now create a new (additional) schemamigration using the ``schemamigration``
  39. management command and follow the instructions::
  40. ./manage.py schemamigration catalogue --auto
  41. To apply the migration you just created, all you have to do is run
  42. ``./manage.py migrate catalogue`` and the new column is added to the product
  43. table in the database.
  44. Customising Products
  45. --------------------
  46. You should inherit from ``AbstractProduct`` as above to alter behaviour for all
  47. your products. Further subclassing is not recommended, because using methods
  48. and attributes of concrete subclasses of ``Product`` are not available unless
  49. explicitly casted to that class.
  50. To model different classes of products, use ``ProductClass`` and
  51. ``ProductAttribute`` instead.
  52. Model customisations are not picked up
  53. --------------------------------------
  54. It's a common problem that you're trying to customise one of Oscar's models,
  55. but your new fields don't seem to get picked up. That is usually caused by
  56. Oscar's models being imported before your customised ones. Django's model
  57. registration disregards all further model declarations.
  58. In your overriding ``models.py``, ensure that you import Oscar's models *after*
  59. your custom ones have been defined. If that doesn't help, you have an import
  60. from ``oscar.apps.*.models`` somewhere that is being executed before your models
  61. are parsed. One trick for finding that import: put ``assert False`` in the relevant
  62. Oscar's models.py, and the stack trace will show you the importing module.
  63. If other modules need to import your models, then import from your local module,
  64. not from Oscar directly.