Skip to content

Instantly share code, notes, and snippets.

@quoidautre
Forked from makeittotop/django_create_user.gist
Created November 8, 2021 13:25
Show Gist options
  • Save quoidautre/f971368f139e34a2c48d15c68f048874 to your computer and use it in GitHub Desktop.
Save quoidautre/f971368f139e34a2c48d15c68f048874 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>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment