In order to learn how to import initial data in database I created models as,
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
after that, I use fixtures in .json format as given below,
[
{
"model": "myapp.person",
"pk": 1,
"fields": {
"first_name": "John",
"last_name": "Lennon"
}
},
{
"model": "myapp.person",
"pk": 2,
"fields": {
"first_name": "Paul",
"last_name": "McCartney"
}
}
]
It throws error on loaddata
File "C:\Python27\lib\site-packages\django\core\serializers\python.py", line 96, in Deserializer
Model = _get_model(d["model"])
django.core.serializers.base.DeserializationError: Problem installing fixture 'I:\DJANGO\library\myapp\fixtures
\bookdata.json': string indices must be integers
But when I use fixture in YAML format as given below,
- model: myapp.person
pk: 1
fields:
first_name: John
last_name: Lennon
- model: myapp.person
pk: 2
fields:
first_name: Paul
last_name: McCartney
It works like a charm.
Now I am confused what was wrong as whole things are just copied from their documentations. I am using windows 32bit, Django 1.9, python 2.7.
I've checked your code in linux mint/django 1.9/python 2.7 and it works fine.
I think the problem may be the codification you have used in your files. Please make sure that the json file uses UTF-8 codification and be careful not to use BOM. Notepad++ editor can determine if the file includes a BOM and remove it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With