python - Import json file to Django model -
i have file in json format, such structure:
{ "admiralty islands": [ [ "up 1 kg", "5.00" ], [ "1 - 10 kg", "10.00" ], ], "afghanistan": [ [ "up 1 kg", "15.00" ], [ "1 - 10 kg", "20.00" ], ], ... }
and 3 models:
class country(models.model): name = models.charfield(max_length=128, unique=true) class weight(models.model): name = models.charfield(max_length=128, unique=true) min_weight = models.integerfield() max_weight = models.integerfield() class shipping(models.model): country = models.foreignkey(country) weight = models.foreignkey(weight) price = models.decimalfield(max_digits=7, decimal_places=2)
what correct way import database using json file?
should convert json file fixture file? relationships between tables? or better write view
like:
f = open('file.json', 'r') obj = simplejson.load(f) o in obj: record = country(name = o) record.save()
but can not figure out how make relations between models
.
or there easier way?
thanks.
use manage.py import fixtures:
python manage.py loaddata fixture.json
Comments
Post a Comment