91

I want to create a fixture file in my Django project.

How can I do this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147

6 Answers6

92

To dump data:

python manage.py dumpdata app.model_name --indent 4 > fixtures/model_name.json

To load data:

python manage.py loaddata fixtures/model_name.json --app app.model_name

--indent X is optional.

Shane Reustle
  • 8,633
  • 8
  • 40
  • 51
Jay Modi
  • 3,161
  • 4
  • 35
  • 52
  • 3
    --indent is indeed optional but a VERY good idea if you want fixxtures that can be checked into git and with actually readable diffs. Without it the fixture all ends up a single line! – Agrajag Nov 23 '21 at 13:07
  • That is true. Although for the large fixture, you have the option to save space. For master data, it's good to have indented fixtures. – Jay Modi Nov 24 '21 at 11:02
75

Read “Providing initial data for models”.

  1. Load some data into a Django-managed database. Simple Python scripts work nicely, Or use the default admin interface.
  2. Use manage.py dumpdata to dump the data into a JSON fixture file. Read "django-admin.py and manage.py".
l0b0
  • 55,365
  • 30
  • 138
  • 223
S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 2
    when linking to docs, better to link to [dev]("https://docs.djangoproject.com/en/dev/ref/django-admin/#dumpdata-appname-appname-appname-model") version? – codervince May 01 '16 at 05:04
  • 4
    The link you provided is no longer valid, here's a new link: [TestCase.fixtures](https://docs.djangoproject.com/en/1.9/howto/initial-data/) – Dan Jun 03 '16 at 06:14
21

You must create a directory in your app named fixtures and put your fixtures files there.

You can write them in json or xml, one easy way to make them is to create some objects in the admin interface and then run manage.py dumpdata. That would dump the data from the objects you created into fixture files. After that you could simply edit those files to suit them to your needs.

https://docs.djangoproject.com/en/1.7/ref/django-admin/#dumpdata-app-label-app-label-app-label-model

If you want to load the fixtures you use manage.py loaddata.

https://docs.djangoproject.com/en/1.7/ref/django-admin/#loaddata-fixture-fixture

You can have fixtures with initial data that would be automatically loaded when you run syncdb, just create a file named initial_data and Django would recognize it.

https://docs.djangoproject.com/en/1.7/howto/initial-data/#automatically-loading-initial-data-fixtures

To use fixtures for testing purposes you must declare them in your test class

https://docs.djangoproject.com/en/1.7/topics/testing/tools/#fixture-loading

jonny
  • 4,264
  • 4
  • 22
  • 29
Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
15

I landed here looking how to do fixtures. I found the following article to be the easiest.

https://code.djangoproject.com/wiki/Fixtures

Add the FIXTURE_DIRS path to your apps's settings.py.

import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
FIXTURE_DIRS = (
   os.path.join(PROJECT_DIR, 'fixtures'),
)

Now dump your current myapp state into a JSON file.

python manage.py dumpdata --format=json myapp > myapp/fixtures/initial_data.json

Thats it, time to test. Drop myapp tables...

./manage.py sqlclear myapp | ./manage.py dbshell

Now re-load the fixtures now...

./manage.py syncdb 
cevaris
  • 5,671
  • 2
  • 49
  • 34
  • 6
    It's also worth noting that you can selectively dump models by using their lowercase path. You can also pretty print by piping through `python -mjson.tool` - `python manage.py dumpdata myapp.mymodel | python -mjson.tool > mymodel.json`. – Rebs Feb 06 '16 at 04:50
  • I get an error when I run `dumpdata` command. `unbound method contribute_to_class() must be called with TextField instance as first argument (got ModelBase instance instead)`. Why? – Hussain Feb 01 '17 at 09:21
  • @Hussain Is the model valid? see https://code.djangoproject.com/wiki/NewbieMistakes#Symptom5 – cevaris Feb 01 '17 at 15:16
5

If you want to dump the entire site, you don't need to specify a fixtures dir in the settings, you can make a fixtures dir in your project and run this

python manage.py dumpdata --format=json > /full-path-to-my-project/fixtures/initial_data.json
Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41
4

I'm currently writing a django module (django-generate_fixtures) to generate clever fixtures, following every related models of one parent object.

It dumps the data as JSON right now, then you can load it the same way as any other fixtures.

FlogFR
  • 729
  • 7
  • 14