77đź‘Ť
Read “Providing initial data for models”.
- Load some data into a Django-managed database. Simple Python scripts work nicely, Or use the default admin interface.
- Use
manage.py dumpdata
to dump the data into a JSON fixture file. Read “django-admin.py and manage.py“.
96đź‘Ť
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.
- [Django]-Are there any plans to officially support Django with IIS?
- [Django]-Django – getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
- [Django]-How do I do a not equal in Django queryset filtering?
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.
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
- [Django]-Django template includes slow?
- [Django]-Running a specific test case in Django when your app has a tests directory
- [Django]-Charts in django Web Applications
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
- [Django]-Running a specific test case in Django when your app has a tests directory
- [Django]-A Better Django Admin ManyToMany Field Widget
- [Django]-Check if celery beat is up and running
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
- [Django]-What is the max size of 'max_length' in Django?
- [Django]-How about having a SingletonModel in Django?
- [Django]-How to get the current URL within a Django template?
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.
- [Django]-Is it possible to pass query parameters via Django's {% url %} template tag?
- [Django]-How to get an ImageField URL within a template?
- [Django]-Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable