Django Friends Meetup, 2014-03-25
django fixtures
they're not that bad!
Florian Demmer (@fdemmer)
what are they?
partial database dumps
via django apps/models
serialization: json, yaml, xml
why should we?
initial state & "configuration"
sample data for designers
and programmers
dropdb and back up in seconds
works offline
looks like...
[ { "pk": 1, "model": "auth.group", "fields": { "name": "content_editor", "permissions": [ [ "add_article", "content", "article" ], ...
how does it work?
manage.py dumpdata > {file}
manage.py loaddata {name}
loaddata name?
fixture name!
without extension
used in multiple apps
fixtures directory, eg. app/fixtures
additional via settings.FIXTURE_DIRS
fixture name
eg. sample_data
app1/fixtures/sample_data.json
app2/fixtures/sample_data.xmlsingle command to load.
init_data fixture
special.
loaded automatically by syncdb & migrate
every time.
(up to 1.6)
to avoid --no-initial-data
(new in 1.5)
1.7?
https://docs.djangoproject.com/en/dev/releases/1.7/
"initial_data fixtures are no longer loaded for apps with migrations; if you want to load initial data for an app, we suggest you do it in a migration."
what is initial_data?
initial_data
whatever is save to overwrite every time.
no: defaults, examples, test data, ...
yes: permission groups, automatically updated data, ...
what about...?
sample_data, test_data
create fixtures, called like what they are!
initial_*
created once, updated by user/customer
loading fixtures
manage.py loaddata initial_data
manage.py loaddata sample_data
...
manage.py loaddata initial_exchangerates
...
creating fixtures
primary keys are dumped
foreign keys are dumped
default is ugly
{"pk": 1, "model": "auth.group", "fields": {"name": "content_editor", "permissions": [161, 163, 164, 165, 157, 158, 172,
...
more useful output
manage.py dumpdata --indent 4 --natural
[ { "pk": 1, "model": "auth.group", "fields": { "name": "content_editor", "permissions": [ [ "add_article", "content", "article" ], ...
because of reasons
indent for readability
formatting for git
valid json (no trailing ,)
natural keys to work with dbs with
different primary keys!
(eg. permissions)
done!
Florian Demmer
fdemmer@gmail.com
questions?
remarks?
4th Django Friends Meetup, 2014-03-25
By Florian Demmer
4th Django Friends Meetup, 2014-03-25
django fixtures
- 1,718