I want to create a parent model user model and a child model book model in Django.
The superuser has been created in advance. (id: 1) Therefore, we create a sampleuser as the second user and associate two child models with that user.
Create a fixtures directory in the application directory and enter as follows. As a caveat, the password for the user model must be the hashed value found by the method described below.
fixtures/sample.json
[
  {
    "model":"app name.user",
    "fields":{
      "id":"2",
      "username":"sampleuser",
      "password":"Hashed string"
    }
  },
  {
    "model":"app name.book",
    "fields":{
      "user":"2",
      "title":"Sample book 1",
      "author":"unknown",
      "price":"0"
    }
  },
  {
    "model":"app name.book",
    "fields":{
      "user":"2",
      "title":"Sapiens",
      "author":"Yuval Noah Harari",
      "price":"32767"
    }
  }
]
Start the console and call make_password to find the value
python manage.py shell
>>> from django.contrib.auth.hashers import make_password
>>> make_password('test')
>>>Hashed value
console
python manage.py loaddata [fixture path]
Installed 1 object(s) from 1 fixture(s)
https://stackoverflow.com/questions/34321075/how-to-add-superuser-in-django-from-fixture/34322435
Write a command
Recommended Posts