Django DeserializationError: "string indices must be integers, not 'str'" when loading large JSON data into SQLite

35 Views Asked by At

I'm encountering a TypeError: string indices must be integers, not 'str' error while trying to load large JSON data into my Django application using the load data command. The data is stored in C:\Users\ambai\PyCharm Projects\Task1\app\fixtures\data.json. My models are defined in models.py.

this is the models.py fields I created based on the raw json format data I used gemini.ai to generate these fields did not done it manually:

from django.db import models

# Create your models here.
class Game(models.Model):
    team=models.CharField(max_length=50)
    team1=models.CharField(max_length=50)
    team_score=models.IntegerField()
    team1_score=models.IntegerField()
    box=models.URLField()
    box1=models.URLField()

class Team(models.Model):
    game_set=models.ForeignKey(Game,on_delete=models.CASCADE,related_name='team_set')
    points=models.IntegerField()
    field_goals=models.IntegerField()
    field_goals_attempts=models.IntegerField()
    field_goals_percentage=models.FloatField()
    three_pointers_made=models.IntegerField()
    three_point_attempts= models.IntegerField()
    three_point_percentage=models.FloatField()
    free_throws_made= models.IntegerField()
    free_throw_attempts= models.IntegerField()
    free_throw_percentage=models.FloatField()
    offensive_rebounds= models.IntegerField()
    total_rebounds= models.IntegerField()
    assists= models.IntegerField()
    personal_fouls= models.IntegerField()
    steals= models.IntegerField()
    turnovers= models.IntegerField()
    blocks= models.IntegerField()

class Field(models.Model):
    game_set = models.ForeignKey(Game, on_delete=models.CASCADE)
    author= models.CharField(max_length=100)
    body=models.TextField(max_length=100)
    created_at= models.DateTimeField(auto_now_add=True)

the error it shows when I run the loaddata command is of Deserialization:

python manage.py loaddata app/fixtures
CommandError: No fixture named 'fixtures' found.
(.venv) PS C:\Users\ambai\PycharmProjects\Task1> python manage.py loaddata app/fixtures/data.json
Traceback (most recent call last):
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\serializers\json.py", line 70, in Deserializer
    yield from PythonDeserializer(objects, **options)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\serializers\python.py", line 114, in Deserializer
    Model = _get_model(d["model"])
                       ~^^^^^^^^^
TypeError: string indices must be integers, not 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\ambai\PycharmProjects\Task1\manage.py", line 22, in <module>
    main()
  File "C:\Users\ambai\PycharmProjects\Task1\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\commands\loaddata.py", line 102, in handle
    self.loaddata(fixture_labels)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\commands\loaddata.py", line 163, in loaddata
    self.load_label(fixture_label)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\commands\loaddata.py", line 251, in load_label
    for obj in objects:
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\serializers\json.py", line 74, in Deserializer
    raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture 'C:\Users\ambai\PycharmProjects\Task1\app/fixtures\data.json':
(.venv) PS C:\Users\ambai\PycharmProjects\Task1> python manage.py makemigrations                 
django.core.serializers.base.DeserializationError: Problem installing fixture 'C:\Users\ambai\PycharmProjects\Task1\app/fixtures\data.json':
(.venv) PS C:\Users\ambai\PycharmProjects\Task1> python manage.py makemigrations
No changes detected
(.venv) PS C:\Users\ambai\PycharmProjects\Task1> python manage.py loaddata app/fixtures/data.json
Traceback (most recent call last):
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\serializers\json.py", line 70, in Deserializer
    yield from PythonDeserializer(objects, **options)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\serializers\python.py", line 114, in Deserializer
    Model = _get_model(d["model"])
                       ~^^^^^^^^^
TypeError: string indices must be integers, not 'str'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\ambai\PycharmProjects\Task1\manage.py", line 22, in <module>
    main()
  File "C:\Users\ambai\PycharmProjects\Task1\manage.py", line 18, in main    
    execute_from_command_line(sys.argv)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\commands\loaddata.py", line 102, in handle
    self.loaddata(fixture_labels)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\commands\loaddata.py", line 163, in loaddata
    self.load_label(fixture_label)
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\management\commands\loaddata.py", line 251, in load_label
    for obj in objects:
  File "C:\Users\ambai\PycharmProjects\Task1\.venv\Lib\site-packages\django\core\serializers\json.py", line 74, in Deserializer
    raise DeserializationError() from exc

1

There are 1 best solutions below

0
nCoder On

you have a typo in you fixture's path C:\Users\ambai\PyCharm Projects\Task1\app/fixtures\data.json should be C:\Users\ambai\PyCharm Projects\Task1\app\fixtures\data.json.