Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add superuser in Django from fixture

Tags:

How to add SUPERuser(not just user) through Django fixtures?

Let's say I wanna have login:admin, password:admin.

like image 564
GriMel Avatar asked Dec 16 '15 20:12

GriMel


People also ask

What is fixture in Django?

fixtures . A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you've already got some data is to use the manage.py dumpdata command.

How do I create a fixture in Django?

You must create a directory in your app named fixtures and put your fixtures files there. You can write them in json or xml, one easy way to make them is to create some objects in the admin interface and then run manage.py dumpdata. That would dump the data from the objects you created into fixture files.


1 Answers

solution 1

On empty database:

python manage.py createsuperuser python manage.py dumpdata auth.User --indent 4 > users.json 

and in users.json You have needed fixtures.

solution 2

./manage.py shell  >>> from django.contrib.auth.hashers import make_password >>> make_password('test') 'pbkdf2_sha256$10000$vkRy7QauoLLj$ry+3xm3YX+YrSXbri8s3EcXDIrx5ceM+xQjtpLdw2oE=' 

and create fixtures file:

[     { "model": "auth.user",         "pk": 1,         "fields": {             "username": "admin",             "password": "pbkdf2_sha256$10000$vkRy7QauoLLj$ry+3xm3YX+YrSXbri8s3EcXDIrx5ceM+xQjtpLdw2oE="             "is_superuser": true,             "is_staff": true,             "is_active": true         }     } ] 
like image 98
Tomasz Jakub Rup Avatar answered Oct 07 '22 13:10

Tomasz Jakub Rup