Python(Django)+Vue.js(Nuxt.js,Vuetify)を使ったシステムの開発です。
今回は前回作ったデータベースのデーブルにFixtureを使って仮のデータを投入します。
初期データを作成する
Djangoで初期データを作成するにはFixtreを使ってみます。
fixtureはあらかじめ導入されており、インストールせずに使うことができます。
fixtureはjson、xml、ymlと3種類の形式対応していて、自分が使いやすいもので書くことができます。今回は記述が簡単なjsonで記述してみます。
まずはfixtureファイルを置くフォルダを作成します。
venvMitsumori > mitsumari > api > fixtures
続けてファイルを作成します。
venvMitsumori > mitsumari > api > fixtures > fixture.json
中身はこんな感じで書きます。テキストエディタやPyCharmなどで貼り付けてください。
[
{"model": "api.item","pk": 1,"fields": {"category": 0, "name": "すごいCPU","description": "", "price" : 50000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 2,"fields": {"category": 0, "name": "普通のCPU","description": "", "price" : 10000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 3,"fields": {"category": 0, "name": "イマイチなCPU","description": "", "price" : 3000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 4,"fields": {"category": 1, "name": "すごいマザーボード","description": "", "price" : 30000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 5,"fields": {"category": 1, "name": "普通のマザーボード","description": "", "price" : 12000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 6,"fields": {"category": 1, "name": "イマイチなマザーボード","description": "", "price" : 5000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 7,"fields": {"category": 2, "name": "すごいビデオカード","description": "", "price" : 60000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 8,"fields": {"category": 2, "name": "普通のビデオカード","description": "", "price" : 8000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 9,"fields": {"category": 2, "name": "イマイチなビデオカード","description": "", "price" : 2000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 10,"fields": {"category": 3, "name": "すごいSSD","description": "", "price" : 20000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 11,"fields": {"category": 3, "name": "普通のHDD","description": "", "price" : 5000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 12,"fields": {"category": 3, "name": "イマイチなSDカード","description": "", "price" : 800,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 13,"fields": {"category": 4, "name": "かっこいいケース","description": "", "price" : 12000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 14,"fields": {"category": 4, "name": "普通のケース","description": "", "price" : 3000,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}},
{"model": "api.item","pk": 15,"fields": {"category": 4, "name": "ダサいケース","description": "", "price" : 1500,"created_at": "2020-01-20T00:00:00Z", "updated_at": "2020-01-20T00:00:00Z"}}
]
データベースに登録する
作成できたら
fixtureコマンドで実際にデータベース上に登録します。
(venvMitsumori) % python manage.py loaddata fixture.json
Installed 15 object(s) from 1 fixture(s)
Installed 15 object(s) from 1 fixture(s) と表示されたらデータの投入はうまく行っています。
正しく登録されか?
ちゃんと登録されているかどうか表示してみます。
(venvMitsumori) % mysql -uroot -p mitsumori-db
Enter password: (password)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 762
Server version: 10.4.6-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [mitsumori-db]> select * from api_item;
+----+----------+-----------------------------------+-------------+-------+----------------------------+----------------------------+
| id | category | name | description | price | created_at | updated_at |
+----+----------+-----------------------------------+-------------+-------+----------------------------+----------------------------+
| 1 | 0 | すごいCPU | | 50000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 2 | 0 | 普通のCPU | | 10000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 3 | 0 | イマイチなCPU | | 3000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 4 | 1 | すごいマザーボード | | 30000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 5 | 1 | 普通のマザーボード | | 12000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 6 | 1 | イマイチなマザーボード | | 5000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 7 | 2 | すごいビデオカード | | 60000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 8 | 2 | 普通のビデオカード | | 8000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 9 | 2 | イマイチなビデオカード | | 2000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 10 | 3 | すごいSSD | | 20000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 11 | 3 | 普通のHDD | | 5000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 12 | 3 | イマイチなSDカード | | 800 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 13 | 4 | かっこいいケース | | 12000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 14 | 4 | 普通のケース | | 3000 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
| 15 | 4 | ダサいケース | | 1500 | 2020-01-20 00:00:00.000000 | 2020-01-20 00:00:00.000000 |
+----+----------+-----------------------------------+-------------+-------+----------------------------+----------------------------+
15 rows in set (0.002 sec)
正しく表示されました。😄
コメント