[Fixed]-How to import a json file to a Django model?

23👍

Use manage.py to import fixtures:

python manage.py loaddata fixture.json

3👍

I don’t see a very clear structure in your json in a sense that it is not explicitly defined anywhere which field should go into which model and how everything is related. So I would recommend just to make an import script in which it manually goes through all of the json and creates the proper model instances.

A good example in my opinion of well structured json is the output of the Django serialization. You can take a look at it here.

2👍

Well for data in json to be populated to the database:

  • You need to map the data fields from json to the database.

  • The best and the preferred (or rather the designed) way of doing this is to use fixtures.

  • For mapping Django Serialization is the way to GO.

1👍

Django has evolved a lot since this question was asked. Under Django 2.x, probably the best way with the least number of introduced dependencies is to use Django’s built-in serializers (as mentioned by @miki725) and data migrations. This will allow your data to get loaded whenever you run migrations (including when migrations are run before running unit tests).

Leave a comment