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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. =======================
  2. How to customise models
  3. =======================
  4. You must first create a local version of the app that you wish to customise. This
  5. involves creating a local app with the same name and importing the equivalent models
  6. from Oscar into it.
  7. Example
  8. -------
  9. Suppose you want to add a video_url field to the core product model. This means that
  10. you want your application to use a subclass of ``oscar.apps.catalogue.models.Product`` which
  11. has an additional field.
  12. The first step is to create a local version of the "catalogue" app. At a minimum, this
  13. involves creating ``catalogue/models.py`` within your project and changing ``INSTALLED_APPS``
  14. to point to your local version rather than Oscar's.
  15. Next, you can modify the ``Product`` model through subclassing::
  16. # yourproject/catalogue/models.py
  17. from django.db import models
  18. from oscar.apps.catalogue.abstract_models import AbstractProduct
  19. class Product(AbstractProduct):
  20. video_url = models.URLField()
  21. from oscar.apps.catalogue.models import *
  22. Make sure to import the remaining Oscar models at the bottom of your file.
  23. The last thing you need to do now is make Django update the database schema and
  24. create a new column in the product table. We recommend to use South migrations
  25. for this (internally Oscar already uses it) so all you need to do is create a
  26. new schema migration. Depending on your setup you should follow one of these
  27. two options:
  28. 1. You **have not** run ``./manage.py migrate`` before
  29. You can simply generate a new initial migration using::
  30. ./manage.py schemamigration catalogue --initial
  31. 2. You **have** run ``./manage.py migrate`` before
  32. You have to copy the ``migrations`` directory from ``oscar/apps/catalogue``
  33. (the same as the ``models.py`` you just copied) and put it into your
  34. ``catalogue`` app.
  35. Now create a new (additional) schemamigration using the ``schemamigration``
  36. management command and follow the instructions::
  37. ./manage.py schemamigration catalogue --auto
  38. To apply the migration you just created, all you have to do is run
  39. ``./manage.py migrate catalogue`` and the new column is added to the product
  40. table in the database.
  41. Customising Products
  42. --------------------
  43. You should inherit from ``AbstractProduct`` as above to alter behaviour for all
  44. your products. Further subclassing is not recommended, because using methods
  45. and attributes of concrete subclasses of ``Product`` are not available unless
  46. explicitly casted to that class.
  47. To model different classes of products, use ``ProductClass`` and
  48. ``ProductAttribute`` instead.
  49. Model customisations are not picked up
  50. --------------------------------------
  51. It's a common problem that you're trying to customise one of Oscar's models,
  52. but your new fields don't seem to get picked up. That is usually caused by
  53. Oscar's models being imported before your customised ones. Django's model
  54. registration disregards all further model declarations.
  55. In your overriding ``models.py``, ensure that you import Oscar's models *after*
  56. your custom ones have been defined. If that doesn't help, you have an import
  57. from ``oscar.apps.*.models`` somewhere that is being executed before your models
  58. are parsed. One trick for finding that import: put ``assert False`` in the relevant
  59. Oscar's models.py, and the stack trace will show you the importing module.
  60. If other modules need to import your models, then import from your local module,
  61. not from Oscar directly.