0

My task is to populate my two existing City and Province model using two json files cities.json' and 'provinces.json. Data in these files are as below.

provinces.json:

[
    {   "model": "salesnetwork.provinces",   
        "id": "1",
        "name": "EA"
    }
]

cities.json:

[
    {   "model": "salesnetwork.cities",
        "id": "1",
        "province_id": "1",
        "name": "Tabriz"
    }
]

Now I'm trying to poplulate my two models with these data. My models are as below.

class Provinces(models.Model):
    name = models.CharField(max_length=255, db_collation="utf8mb3_unicode_ci")

    class Meta:
        db_table = "provinces"


class Cities(models.Model):
    province = models.ForeignKey("Provinces", models.DO_NOTHING)
    name = models.CharField(max_length=255, db_collation="utf8mb3_unicode_ci")

    class Meta:
        db_table = "cities"

(My bad for naming my model Cities instead of City and Provinces instead of 'Province'; I've created these models via inspectdb command and since I didn't know if changing these model names could cause problems or not, I decided to leave the names as they were). When I try using command py .\manage.py loaddata provinces.json I get the following error:

Traceback (most recent call last):
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\serializers\json.py", line 69, in Deserializer
    objects = json.loads(stream_or_string)
  File "C:\Users\Vahid Moradi\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 335, in loads
    raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)

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

Traceback (most recent call last):
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\manage.py", line 22, in <module>
    main()
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\commands\loaddata.py", line 102, in handle
    self.loaddata(fixture_labels)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\commands\loaddata.py", line 163, in loaddata
    self.load_label(fixture_label)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\commands\loaddata.py", line 251, in load_label
    for obj in objects:
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.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 'D:\Projects\Navid Motor\Website\Django\NavidMotor.com\provinces.json'::

and by the looks of it, this time the problem is json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0), which I have no idea how to resolve and didn't find any instruction on how to solve this issue. I did study the following questions, but all were for problems relating json library inside .py files, and since my problem occurs in command line using manage.py commands, I couldn't apply those solutions for my problem. Possible simillar questions, but could not use the answers given:

  1. First
  2. Second
  3. Third
Vahid
  • 249
  • 4
  • 12

1 Answers1

1

Try to paste json into new file in vscode and save it. Errorjson.decoder.JSONDecodeError: Unexpected UTF-8 BOM because your json files have UTF8-BOM encoding: https://www.coderedcorp.com/blog/how-to-dump-your-django-database-and-load-it-into-/

necro
  • 80
  • 1
  • 6