https://medium.com/@ardho/migration-and-seeding-in-django-3ae322952111
Simple 1 :
manually create a json file via build in django cmd, then use
python manage.py loaddata <file>
simple file can be created using
For example, we want to create two Admin object and make the fixture file. So make it a regular model for admin, then we could run dumpdata
command. First, create the object of the model (we’ll use Django shell, just choose which one is most comfortable). (Admin model in admin_birpen app)
python3 manage.py shell
>>> from admin_birpen.models import Admin
>>> Admin(pk=1, username="@adminPPL").save()
>>> Admin(pk=2, username="@adminPacil").save()
Then we can dump the created data in JSON file a.k.a. the fixture file in seed/0008_Admin.json with 4 indentation space.
<admin_birpen> is the app name
python3 manage.py dumpdata admin_birpen --indent 4 > seed/0008_Admin.json
Or generate complex json file using
django-seed plugin
pip install django-seed
For installation, just run pip install django-seed
. More documentation about django-seed visit this link. So I will simulate generate the fixture for the model Pengumuman. Long short story, Pengumuman model has more than 13 fields with quite a lot of model dependencies.
So how do we generate the fixture? Simple, just run python3 manage.py seed pengumuman --number=2
, so from this command, we will generate 2 object models in the pengumuman application. This command is not to generate fixture directly, but rather to make the object directly inputted to the database. So how do we generate the fixture? As previously explained, we can use dumpdata command!
No comments:
Post a Comment