10 examples of 'django get_user_model' in Python

Every line of 'django get_user_model' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
14def get_user_model():
15 """
16 Get the Django user model class in a backwards compatible way to previous
17 Django version that don't provide this functionality. The result is cached
18 after the first call.
19
20 Returns the user model class.
21 """
22 global _user_model
23
24 if not _user_model:
25 # we have to import the user model here because we can't be sure
26 # that the app providing the user model is fully loaded.
27 try:
28 from django.contrib.auth import get_user_model
29 except ImportError:
30 from django.contrib.auth.models import User
31 else:
32 User = get_user_model()
33 _user_model = User
34 return _user_model
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

155def get_user(self, user_id):
156 try:
157 user = UserModel._default_manager.get(pk=user_id)
158 except UserModel.DoesNotExist:
159 return None
160 return user if self.user_can_authenticate(user) else None
31def is_django_user_model(user):
32 """
33 Checks if a user model is compatible with Django's
34 recent changes. Django requires User models to have
35 an int pk, so we check here if it has (mongoengine hasn't)
36 """
37 try:
38 if hasattr(user, 'pk'):
39 int(user.pk)
40 else:
41 int(user)
42 except (ValueError, TypeError):
43 return False
44 return True
31def get_user_queryset():
32 return get_user_model().objects.all()
152@classmethod
153def get_user(cls, pk):
154 try:
155 return cls.user_model().objects.get(pk=pk)
156 except cls.user_model().DoesNotExist:
157 return None
13def get_user(self, user_id):
14 user = super(FilteredModelBackend, self).get_user(user_id)
15 return self.filter_user(user)
67@property
68def user_model(self):
69 return self.server.app.m.user
83def get(self, *args, **kwargs):
84 try:
85 return self.get_query_set().get(*args, **kwargs)
86 except self.model.DoesNotExist:
87 # ModelBackend expects this exception
88 raise self.dj_model.DoesNotExist
36@classmethod
37def user_model(cls):
38 return cls.user.property.argument
25def authenticate(self, request, username=None, password=None, **kwargs):
26 if '@' in username:
27 kwargs = {'email': username}
28 else:
29 kwargs = {'username': username}
30 try:
31 user = get_user_model().objects.get(**kwargs)
32 if user.check_password(password):
33 return user
34 except get_user_model().DoesNotExist:
35 return None

Related snippets