Django : ValueError on loading fixture

6.5k Views Asked by At

Im trying to load a fixture which looks like this :

some_filename.json

[
{
    "fields": {
        "blockName": "Home & Tech", 
        "catName": "Home", 
        "catUrl": "http://www.buyam.com.ng/catalog/home/35", 
        "prodDesc": "Jojo Pet food is made with added nutrition to meet the demands of a grown up dog that's highly active and needs loads of energy and nutrition. By gaining the required energy with this food, dogs can live life to the optimum. This food can prove to be better as it gives dogs the required blend of fibers that can keep the stomach healthy and the patented X-shaped kibbles that are good to maintain clean and strong teeth till the gum line.", 
        "prodName": "Jo-Jo Dog Food - Tripe Mix 400g", 
        "prodTypeName": "Pet Supplies", 
        "prodTypeUrl": "http://www.buyam.com.ng/catalog/pet-supplies/419", 
        "prodUrl": "http://www.buyam.com.ng/catalog/pet-supplies/419/jo-jo-dog-food-tripe-mix-400g"
    }, 
    "model": "kudisavers.product", 
    "pk": 1
},
{
...
...
}
]

But I get the following error :

    Traceback (most recent call last):
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\serializers\json.py", line 74, in Deserializer
    objects = json.loads(stream_or_string)
  File "C:\Python34\lib\json\__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\commands\loaddata.py", line 61, in handle
    self.loaddata(fixture_labels)
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\commands\loaddata.py", line 91, in loaddata
    self.load_label(fixture_label)
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\commands\loaddata.py", line 142, in load_label
    for obj in objects:
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\serializers\json.py", line 81, in Deserializer
    six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\utils\six.py", line 549, in reraise
    raise value.with_traceback(tb)
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\serializers\json.py", line 74, in Deserializer
    objects = json.loads(stream_or_string)
  File "C:\Python34\lib\json\__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
django.core.serializers.base.DeserializationError: Problem installing fixture 'D:\MyWork\Python\Django_Projects\kudisavers\fixtures\buyam-fixture.json': Expecting value: line 1 column 1 (char 0)

This is how I created the json :

file = open('some_filename.json', 'wb')
file.write(json.dumps(data, sort_keys=True, indent=4))
file.close()

And I load the fixture from the cmd like this :

python manage.py loaddata some_filename.json 

The values and keys seem alright to me. There are no blank values in the entire json. What might be wrong? Thanks :)

2

There are 2 best solutions below

2
On BEST ANSWER

You should remove the json format, the proper json file looks like this:

[{"pk": 1, "model": "contenttypes.contenttype", "fields": {"model": "permission", "name": "permission", "app_label": "auth"}},...]
1
On

You should use dumpdata to create your fixtures.

From the docs:

Outputs to standard output all data in the database associated with the named application(s).

If no application name is provided, all installed applications will be dumped.

The output of dumpdata can be used as input for loaddata.

Note that dumpdata uses the default manager on the model for selecting the records to dump. If you’re using a custom manager as the default manager and it filters some of the available records, not all of the objects will be dumped.

The --all option may be provided to specify that dumpdata should use Django’s base manager, dumping records which might otherwise be filtered or modified by a custom manager.