'Speed up object creation

I have an API call which return an json file like 1000s of this:

{
    "date": "2021-02-27T00:00:00+00:00",
    "value": 11590,
    "countryName": "Netherlands",
    "countryCode": "NL"
},
{
    "date": "2021-02-27T00:00:00+00:00",
    "value": 88,
    "countryName": "Belgium",
    "countryCode": "BE"
}

I have some code which creates entries and links it through a foreign key:

for country in response['countryPlots']:
    country['parent'] = parent.pk
    countrydata = CountrySerializer(data=country)
    countrydata.is_valid(raise_exception=True)
    countrydata.save()

But it's too slow. I have to do millions of these creations.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source