{"id":1243,"date":"2014-01-10T21:26:17","date_gmt":"2014-01-10T21:26:17","guid":{"rendered":"http:\/\/joelinoff.com\/blog\/?p=1243"},"modified":"2014-01-10T21:58:32","modified_gmt":"2014-01-10T21:58:32","slug":"how-to-add-a-superuser-in-django-1-6-from-the-command-line","status":"publish","type":"post","link":"https:\/\/joelinoff.com\/blog\/?p=1243","title":{"rendered":"How to add a superuser in Django 1.6 from the command line"},"content":{"rendered":"<p>This python script shows how to add a super user and display all super users in your Django 1.6 installation from the command line. I wrote it to support automated configuration of my django installations.<br \/>\n<!--more--><br \/>\nThe script file must reside in the settings.py directory but that can be changed by changing the &#8216;settings&#8217; argument at line 9.<\/p>\n<pre class=\"lang:python decode:true \" title=\"make-user.py\" ># Usage:\r\n#    $ # set your virtual environment if necessary\r\n#    $ cd &lt;django-project&gt;\/&lt;project&gt;\r\n#    $ python make-user bigbob secret true\r\nimport os\r\nimport sys\r\n\r\n# The new way to setup your environment.\r\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')\r\nfrom django.contrib.auth.models import User\r\n\r\n# No fancy error checking.\r\nassert len(sys.argv) == 4\r\nusername = sys.argv[1]\r\npassword = sys.argv[2]\r\nsuperuser = sys.argv[3]  # true or false\r\n\r\n# Create the user, make them a superuser if the 3rd\r\n# argument is true.\r\nuser = User(username='%s' % (username))\r\nuser.set_password('%s' % (password))\r\nif superuser.lower() in ['true', 't']:\r\n    user.is_superuser = True\r\n    user.is_staff = True\r\n\r\nuser.save()\r\n\r\n# List the superusers.\r\nfrom django.db.models import Q\r\nsuperusers = [str(user) for user in User.objects.filter(Q(is_superuser=True)).distinct()]\r\nprint('%d superusers: %s' % (len(superusers), ', '.join(superusers)))<\/pre>\n<p>Note that there are many other user fields (like email) that it does not set.<\/p>\n<p>I hope that you find it helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This python script shows how to add a super user and display all super users in your Django 1.6 installation from the command line. I wrote it to support automated configuration of my django installations.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[5,7,16],"tags":[],"_links":{"self":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1243"}],"collection":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1243"}],"version-history":[{"count":7,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1243\/revisions"}],"predecessor-version":[{"id":1250,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1243\/revisions\/1250"}],"wp:attachment":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}