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.

use_a_custom_user_model.rst 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ==============================
  2. How to use a custom user model
  3. ==============================
  4. You can specify a custom user model in the ``AUTH_USER_MODEL`` setting.
  5. Oscar will dynamically adjust the account profile summary view and
  6. profile editing form to use the fields from your custom model.
  7. Before Django 1.5, the recommended technique for adding fields to users was to
  8. use a one-to-one "profile" model specified in the ``AUTH_PROFILE_MODULE``.
  9. While this setting was removed from Django in Django 1.7, Oscar continues to
  10. support it and will add relevant fields to the profile form.
  11. Hence profiles can be used in combination with custom user models.
  12. That doesn't mean it's a good idea.
  13. Restrictions
  14. ------------
  15. Oscar does have some requirements on what fields a user model has. For
  16. instance, the authentication backend requires a user to have an 'email' and
  17. 'password' field. Oscar also assumes that the ``email`` field is unique, as
  18. this is used to identify users.
  19. Oscar ships with its own abstract user model that supports the minimum
  20. fields and methods required for Oscar to work correctly. New Oscar projects
  21. are encouraged to subclass this User model.
  22. Migrations
  23. ----------
  24. It has previously been suggested to set ``db_table`` of the model to
  25. ``auth_user`` to avoid the migrations from breaking. This issue has been fixed
  26. and migrations are now using ``AUTH_USER_MODEL`` and ``AUTH_USER_MODEL_NAME``
  27. which will use ``db_table`` name of the user model provided by
  28. ``get_user_model()``.
  29. This works in the instances where you are using the default ``auth.User`` model
  30. or when you use a custom user model from the start. Switching over from
  31. ``auth.User`` to a custom model after having applied previous migration of
  32. Oscar will most likely require renaming the ``auth_user`` table to the new user
  33. table in a manual migration.
  34. Example
  35. -------
  36. If you want to use ``oscar.apps.customer.abstract_model.AbstractUser``
  37. which has ``email`` as an index, and want to customise some of the methods on
  38. ``User`` model, say, ``get_full_name`` for Asian names, a simple approach is
  39. to create your own ``user`` module::
  40. # file: your-project/apps/user/models.py
  41. from django.db import models
  42. from oscar.apps.customer.abstract_models import AbstractUser
  43. class User(AbstractUser):
  44. def get_full_name(self):
  45. full_name = '%s %s' % (self.last_name.upper(), self.first_name)
  46. return full_name.strip()
  47. Then add this ``user`` app to the ``INSTALLED_APPS`` list. Beside that we
  48. need to tell ``django`` to use our customised user model instead of the
  49. default one as the authentication model [1]_::
  50. # use our own user model
  51. AUTH_USER_MODEL = "user.User"
  52. After the migration, a database table called ``user_user`` will be created based
  53. on the schema defined inside of
  54. ``oscar.apps.customer.abstract_models.AbstractUser``.
  55. .. [1] https://docs.djangoproject.com/en/stable/ref/settings/#auth-user-model