Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django loaddata it throws errors for json format but work properly for yaml format, why?

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.

like image 989
arun pal Avatar asked Dec 31 '15 19:12

arun pal


1 Answers

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.

like image 127
Euribates Avatar answered Oct 21 '22 17:10

Euribates