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

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