Help us understand the problem. What is going on with this article?

Djangoの初期データ投入機能についてメモ

More than 3 years have passed since last update.

fixture というものがある

詳しくはここDjangoのドキュメント

JSON, XML, YAMLで書ける

使い方

1. このような構造にする

.
└── mysite
    ├── db.sqlite3
    ├── manage.py
    ├── mysite
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── polls
        ├── __init__.py
        ├── admin.py
        ├── apps.py
        ├── migrations
        ├── models.py
        ├── templates
        ├── tests.py
        ├── urls.py
        ├── views.py
        └── fixtures # 追加

2. fixturesディレクトリに投入したいデータを書いたJSONファイル追加

fixtures
├── data1.json
├── data2.json
├── data3.json
└── data4.json

3. loaddata を使って読み込む

$ python manage.py loaddata polls/fixtures/data1.json

4. 反対にDBのデータを出力したいときは dumpdata を使う

$ python manage.py dumpdata --indent=2 --format=json polls.modelname > polls/fixtures/data.json

日本語が含まれている場合、エスケープされてしまうので、エスケープされたままだと困る場合は変換する必要がある。

mimikun
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした