1π
To create your fixture files start with an empty database and then add some data to your database using the djanog-admin or the django shell or even pure SQL. After that you can do a
python manage.py dumpdata # to dump all your data, or python manage.py dumpdata app_name # to dump all data of a specific app, or python manage.py dumpdata app_name.model_name # to dump all data of a specific model
The above will print data to your stdout. In order to write it to a file use a redirect (>), for instance
python manage.py dumpdata auth.User > user_fixture.json
Update: I just saw that you are using Windows β remember to load your fixtures using a backslash (\
).
4π
As for me, the matter is the suffix of the output file.
python manage.py dumpdata -o my_dump
python manage.py loaddata < my_dump # it fails
# change the file name my_dump to my_dump.json
python manage.py loaddata < my_dump.json # it works
So, I guess the dumpdata
implicitly use json
as output format. While loaddata
needs a format tip from file name suffix.
- [Django]-"global name '_' is not defined" during raising ValidationError
- [Django]-Background process in GAE
- [Django]-How do I filter API results by a related model attribute using Tastypie?
- [Django]-How to filter model with a list containing the field and the value?
- [Django]-Django DB Design β Maintaining common and historical data
2π
We can do this like below
models.py
from django import models
class Model_Name(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
fixures
[
{
"model": "app_name.model_name",
"pk": 1,
"fields": {
"name": "My app name"
}
}
]
command
./manage.py loaddata app_name/fixtures/model_name.json
Output: Installed 1 object(s) from 1 fixture(s)
1π
For those who still having this issues.
The error is telling you that:
CommandError: Problem installing fixture βMyTableβ: sql is not a known serialization format.
You could look on it as:
sql is not a known serialization format.
It will not accept any file not ending on .json
.
So to solve your problem use this command:
$ python manage.py loaddata <your_dump_filename>.json
- [Django]-Django Registration Number of users logged in
- [Django]-Django Registration Redux: how to change the unique identifier from username to email and use email as login
- [Django]-Django migrations: how to generate database from current state of the models (good old syncdb βall)
- [Django]-Django ImageField not uploading the image