[Django]-Django – cannot get loaddata to work

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 (\).

πŸ‘€Serafeim

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.

πŸ‘€W.Perrin

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
πŸ‘€Shift 'n Tab

Leave a comment