10 examples of 'create super user in django' in Python

Every line of 'create super user in django' 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
20def create_user(self, username='bouke@example.com', password='secret', **kwargs):
21 user = self.User.objects.create_user(username=username, email=username, password=password, **kwargs)
22 self._passwords[user] = password
23 return user
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

24def create_user(**kwargs):
25 global user_number
26
27 password = kwargs.pop("password", None)
28 if "username" not in kwargs:
29 kwargs["username"] = "test%s" % user_number
30 user_number += 1
31
32 from django.contrib.auth.models import User
33
34 user = User(**kwargs)
35 if password:
36 user.set_password(password)
37 user.save()
38 return user
339def create_user(
340 username, email, password, is_staff=False, is_superuser=False, base_cms_permissions=False, permissions=None,
341):
342 from django.contrib.auth.models import Permission
343
344 User = get_user_model() # NOQA
345
346 try:
347 if User.USERNAME_FIELD == "email":
348 user = User.objects.get(**{User.USERNAME_FIELD: email})
349 else:
350 user = User.objects.get(**{User.USERNAME_FIELD: username})
351 except User.DoesNotExist:
352 user = User()
353
354 if User.USERNAME_FIELD != "email":
355 setattr(user, User.USERNAME_FIELD, username)
356
357 try:
358 user.email = email
359 except AttributeError:
360 pass
361 user.set_password(password)
362 if is_superuser:
363 user.is_superuser = True
364 if is_superuser or is_staff:
365 user.is_staff = True
366 user.is_active = True
367 user.save()
368 if user.is_staff and not is_superuser and base_cms_permissions:
369 user.user_permissions.add(Permission.objects.get(codename="add_text"))
370 user.user_permissions.add(Permission.objects.get(codename="delete_text"))
371 user.user_permissions.add(Permission.objects.get(codename="change_text"))
372 user.user_permissions.add(Permission.objects.get(codename="publish_page"))
373
374 user.user_permissions.add(Permission.objects.get(codename="add_page"))
375 user.user_permissions.add(Permission.objects.get(codename="change_page"))
376 user.user_permissions.add(Permission.objects.get(codename="delete_page"))
377 if is_staff and not is_superuser and permissions:
378 for permission in permissions:
379 user.user_permissions.add(Permission.objects.get(codename=permission))
380 return user
226def admin_create_user(creator, username, password, is_superuser = False, first_name=None, last_name=None, email=None):
227 if not creator.is_superuser:
228 raise PermissionDenied('only superusers may create other users.')
229
230 # Look for other users with the same username first, to avoid the evil IntegrityError
231 if User.objects.filter(username=username).exists():
232 raise ValueError('a user with that username already exists.')
233
234 user = User.objects.create_user(username, email=email, password=password)
235 user.is_superuser = is_superuser
236 user.first_name = first_name
237 user.last_name = last_name
238 user.save()
239 return user
72def test_create_super_user_raises_error_on_false_is_superuser(self):
73 """Warn developer when creating superuse without is_superuser"""
74 error = 'Superuser must have is_superuser=True.'
75 with self.assertRaisesMessage(ValueError, error):
76 User.objects.create_superuser(
77 email='test@test.com',
78 password='test',
79 is_superuser=False,
80 )
62def create_user_(self):
63 user = User(
64 username = self.username,
65 email = self.details['email']
66 )
67 if 'first_name' in self.data:
68 user.first_name = self.data['first_name']
69 if 'last_name' in self.data:
70 user.last_name = self.data['last_name']
71 user.set_password(random_password())
72 user.save()
73 self.user = user
112def create_user(self):
113 return None
214def create_user(**kwargs):
215 "Create an user along with her dependencies"
216 user = UserFactory.create(**kwargs)
217 password = kwargs.pop('password', None)
218 if password:
219 user.set_password(password)
220 user.is_active = True
221 user.save()
222 return user
32def create_super_admin(email, password):
33 user = DataManager.create_user([email, password], is_verified=True)
34 user.is_super_admin = True
35 user.is_admin = True
36 save_to_db(user, "User updated")
37 return user
45def user_create_(superuser=False, anonymous=False, attributes=None):
46 """
47 :rtype: AbstractBaseUser
48 """
49 if anonymous:
50 return AnonymousUser()
51
52 attributes = attributes or {}
53 manager = user_model._default_manager
54
55 username = (('admin' if superuser else 'user') + '_' + get_stamp())
56
57 kwargs = {
58 'username': username,
59 'email': '%s@example.com' % username,
60 'password': 'password',
61 }
62
63 kwargs.update(attributes)
64
65 if superuser:
66 user = manager.create_superuser(**kwargs)
67 else:
68 user = manager.create_user(**kwargs)
69
70 # Keep plain password for test purposes.
71 user.password_plain = kwargs['password']
72
73 return user

Related snippets