I have a bunch of object in a response object, which I save into the database. Doing it object by object is very slow since that essentially means if its 30k objects, its gonna make 30k commits to the database afaik.
Example 1:
for obj in response['RESULTS']:
_city = City.objects.create(
id=obj['id'],
name=obj['name'],
shortname=obj['shortname'],
location=obj['location'],
region=region_fk
)
_events = Event.objects.get(pk=obj['Event'])
_events.city_set.add(_city)
My new approach to implement bulk_create()
is something like this:
Example 2:
bulk_list = []
for obj in response['RESULTS']:
# get the foreignkey instead of duplicating data
if obj.get('Region'):
region_fk = Region.objects.get(pk=obj['Region'])
bulk_list.append(
City(
id=obj['id'],
name=obj['name'],
shortname=obj['shortname'],
location=obj['location'],
region=region_fk
)
)
bulk_save = City.objects.bulk_create(bulk_list)
While this is alot faster than my previous attempt it has a problem, now I dont know how to add my M2M relationships.
models.py
class City(models.Model):
id = models.CharField(primary_key=True, max_length=64)
name = models.CharField(max_length=32)
shortname = models.CharField(max_length=32)
location = models.CharField(max_length=32)
region = models.ForeignKey(max_length=32)
events = models.ManyToManyField(Event)
class Event(models.Model):
id = models.CharField(primary_key=True, max_length=64)
description = models.TextField()
date = models.DateTimeField()
class Region(models.Model):
id = models.IntegerField(primary_key=True)
Questions
I have looked around on stackoverflow and have found some examples but I dont understand them completely.. it seems like most answers talks about
bulk_create the M2M relations aswell via a through
model, and im not sure thats what I am looking for.
- How can I add those M2M relations?
- Please break it down so I can understand, I want to learn :-)
Any help or pointers are highly appreciated. Thank you.
Other information
I run:
- postgresql
- django==1.11
Related posts
- Django setting many_to_many object while doing a bulk_create
- Django `bulk_create` with related objects
Django docs on this topic
Response example:
"RESULT": [
{
"City": [
{
"id": "349bc6ab-1c82-46b9-889e-2cc534d5717e",
"name": "Stockholm",
"shortname": "Sthlm",
"location": "Sweden",
"region": [
2
],
"events": [
{
"id": "989b6563-97d2-4b7d-83a2-03c9cc774c21",
"description": "some text",
"date": "2017-06-19T00:00:00"
},
{
"id": "70613514-e569-4af4-b770-a7bc9037ddc2",
"description": "some text",
"date": "2017-06-20T00:00:00"
},
{
"id": "7533c16b-3b3a-4b81-9d1b-af528ec6e52b",
"description": "some text",
"date": "2017-06-22T00:00:00"
},
}
}
]