[PYTHON] How to use fixture in Django to populate sample data associated with a user model

Target

I want to create a parent model user model and a child model book model in Django.

Premise

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.

result

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"
    }
  }
]

How to hash a password

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

Finally input with the following command

console


python manage.py loaddata [fixture path]
Installed 1 object(s) from 1 fixture(s)

References

https://stackoverflow.com/questions/34321075/how-to-add-superuser-in-django-from-fixture/34322435

Postscript

Write a command

Recommended Posts

How to use fixture in Django to populate sample data associated with a user model
How to use CUT command (with sample)
Implement a Custom User Model in Django
How to create sample CSV data with hypothesis
How to develop a cart app with Django
How to use tkinter with python in pyenv
How to create a Rest Api in Django
[PyTorch] Sample ⑧ ~ How to build a complex model ~
How to convert / restore a string with [] in python
How to use xgboost: Multi-class classification with iris data
How to get multiple model objects randomly in Django
How to get a logged-in user with Django's forms.py
A memorandum on how to use keras.preprocessing.image in Keras
How to use bootstrap in Django generic class view
How to use the model learned in Lobe in Python
How to use Decorator in Django and how to make it
How to reference static files in a Django project
How to use Spacy Japanese model in Google Colaboratory
How to use python multiprocessing (continued 3) apply_async in class with Pool as a member
[Django] Manage settings like writing in settings.py with a model
How to use Laravel-like ORM / query builder Orator in Django
How to output a document in pdf format with Sphinx
How to use the __call__ method in a Python class
How to check ORM behavior in one file with django
How to update user information when logging in to Django RemoteUserMiddleware
Ingenuity to handle data with Pandas in a memory-saving manner
[Django] How to give input values in advance with ModelForm
How to generate a query using the IN operator in Django
How to create a record by pasting a relation to the inheriting source Model in the Model inherited by Django
Python: How to use async with
How to use classes in Theano
How to use SQLite in Python
How to deal with imbalanced data
How to deal with imbalanced data
How to create a heatmap with an arbitrary domain in Python
Here's a brief summary of how to get started with Django
How to use python put in pyenv on macOS with PyCall
[Introduction to Python] How to use the in operator in a for statement?
<Pandas> How to handle time series data in a pivot table
How to reflect CSS in Django
How to get started with Django
How to use Mysql in python
How to Data Augmentation with PyTorch
How to use ChemSpider in Python
How to use FTP with Python
How to use PubChem in Python
How to create a large amount of test data in MySQL? ??
How to deploy a Django app on heroku in just 5 minutes
How to return the data contained in django model in json format and map it on leaflet
Learn how to use Docker through building a Django + MySQL environment
How to authenticate with Django Part 2
How to authenticate with Django Part 3
Django
Django Management Command Duplicate Launch Decorator
Output user information etc. to Django log
Implement a Custom User Model in Django
Implementation of custom user model authentication in Django REST Framework with djoser
[Small story] How to save matplotlib graphs in a batch with Jupyter
How to make a model for object detection using YOLO in 3 hours
[Python] Explains how to use the range function with a concrete example
How to send a visualization image of data created in Python to Typetalk
How to install python package in local environment as a general user
How to store CSV data in Amazon Kinesis Streams with standard input
How to use PyCharm with Glue development endpoints running inside a VPC