Connect to another database and dumpdata from it in json format

Question:

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:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoreserializersjson.py", line 69, in Deserializer
    objects = json.loads(stream_or_string)
  File "C:UsersVahid MoradiAppDataLocalProgramsPythonPython310libjson__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:ProjectsNavid MotorWebsiteDjangoNavidMotor.commanage.py", line 22, in <module>
    main()
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.commanage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagement__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagement__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagementbase.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagementbase.py", line 448, in execute
    output = self.handle(*args, **options)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagementcommandsloaddata.py", line 102, in handle
    self.loaddata(fixture_labels)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagementcommandsloaddata.py", line 163, in loaddata
    self.load_label(fixture_label)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagementcommandsloaddata.py", line 251, in load_label
    for obj in objects:
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoreserializersjson.py", line 74, in Deserializer
    raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture 'D:ProjectsNavid MotorWebsiteDjangoNavidMotor.comprovinces.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
Asked By: Vahid

||

Answers:

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-/

Answered By: necro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.