Skip to content

Instantly share code, notes, and snippets.

@makeittotop
Last active April 15, 2023 11:43
Show Gist options
  • Save makeittotop/f0df9644088b4d54cbcc to your computer and use it in GitHub Desktop.
Save makeittotop/f0df9644088b4d54cbcc to your computer and use it in GitHub Desktop.
Create / remove user / superuser in a django app
>>> from django.contrib.auth.models import User
# Create a regular user 'foo'
>>> user = User.objects.create_user('foo', 'foo.bar@xxx.com', 'bar')
# List all users
>>> User.objects.all()
[<User: admin>, <User: abegail>, <User: foo>]
>>> User.objects.all()[1].is_superuser
True
>>> User.objects.all()[2].is_superuser
False
# Drop a user from the db
>>> User.objects.all()[2].delete()
>>> User.objects.all()
[<User: admin>, <User: abegail>]
# Create a superuser
>>> user = User.objects.create_superuser('burr', 'burr@buzz.com', 'buzz')
>>> User.objects.all()
[<User: admin>, <User: abegail>, <User: burr>]
>>> User.objects.all()[2].delete()
>>> User.objects.all()
[<User: admin>, <User: abegail>]
@berbang04
Copy link

truly helpful it really helped me , thank u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment