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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. The last thing you need to do now is make Django update the database schema and
  22. create a new column in the product table. We recommend to use South migrations
  23. for this (internally Oscar already uses it) so all you need to do is create a
  24. new schema migration. Depending on your setup you should follow one of these
  25. two options:
  26. 1. You **have not** run ``./manage.py migrate`` before
  27. You can simply generate a new initial migration using::
  28. ./manage.py schemamigration catalogue --initial
  29. 2. You **have** run ``./manage.py migrate`` before
  30. You have to copy the ``migrations`` directory from ``oscar/apps/catalogue``
  31. (the same as the ``models.py`` you just copied) and put it into your
  32. ``catalogue`` app.
  33. Now create a new (additional) schemamigration using the ``schemamigration``
  34. management command and follow the instructions::
  35. ./manage.py schemamigration catalogue --auto
  36. To apply the migration you just created, all you have to do is run
  37. ``./manage.py migrate catalogue`` and the new column is added to the product
  38. table in the database.
  39. Customising Products
  40. --------------------
  41. You should inherit from ``AbstractProduct`` as above to alter behaviour for all
  42. your products. Further subclassing is not recommended, because using methods
  43. and attributes of concrete subclasses of ``Product`` are not available unless
  44. explicitly casted to that class.
  45. To model different classes of products, use ``ProductClass`` and
  46. ``ProductAttribute`` instead.