How to create a Django superuser if it doesn't exist non-interactively?
<p>I want to automate creation of Django users via a Bash script. I found this snippet which almost suits my needs:   </p>

<pre><code>echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" |\
    python manage.py shell
</code></pre>

<p>How can I modify it so that it's a nop if the user already exists?</p>

<p>You can use <code>filter()</code> and <code>exists()</code> to check whether the a user with that username exists before you create it.</p>

<pre><code>if not User.objects.filter(username="admin").exists():
    User.objects.create_superuser('admin', 'admin@example.com', 'pass')
</code></pre>

python django django-users