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.
 
 
 
 

65 lines
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.apps import apps
  4. from django.conf import settings
  5. from django.core import checks
  6. def check_user_model(**kwargs):
  7. errors = []
  8. cls = apps.get_model(settings.AUTH_USER_MODEL)
  9. # Check that REQUIRED_FIELDS is a list
  10. if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)):
  11. errors.append(
  12. checks.Error(
  13. "'REQUIRED_FIELDS' must be a list or tuple.",
  14. hint=None,
  15. obj=cls,
  16. id='auth.E001',
  17. )
  18. )
  19. # Check that the USERNAME FIELD isn't included in REQUIRED_FIELDS.
  20. if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS:
  21. errors.append(
  22. checks.Error(
  23. ("The field named as the 'USERNAME_FIELD' "
  24. "for a custom user model must not be included in 'REQUIRED_FIELDS'."),
  25. hint=None,
  26. obj=cls,
  27. id='auth.E002',
  28. )
  29. )
  30. # Check that the username field is unique
  31. if not cls._meta.get_field(cls.USERNAME_FIELD).unique:
  32. if (settings.AUTHENTICATION_BACKENDS ==
  33. ['django.contrib.auth.backends.ModelBackend']):
  34. errors.append(
  35. checks.Error(
  36. "'%s.%s' must be unique because it is named as the 'USERNAME_FIELD'." % (
  37. cls._meta.object_name, cls.USERNAME_FIELD
  38. ),
  39. hint=None,
  40. obj=cls,
  41. id='auth.E003',
  42. )
  43. )
  44. else:
  45. errors.append(
  46. checks.Warning(
  47. "'%s.%s' is named as the 'USERNAME_FIELD', but it is not unique." % (
  48. cls._meta.object_name, cls.USERNAME_FIELD
  49. ),
  50. hint=('Ensure that your authentication backend(s) can handle '
  51. 'non-unique usernames.'),
  52. obj=cls,
  53. id='auth.W004',
  54. )
  55. )
  56. return errors