python - How to efficiently fetch objects after created using bulk_create function of Django ORM? -
i have insert multiple objects in table, there 2 ways that-
1) insert each 1 using save()
. in case there n sql db queries n objects.
2) insert of them using bulk_create()
. in case there one sql db query n objects.
clearly, second option better , hence using that. problem bulk__create
not return ids of inserted objects hence can not used further create objects of other models have foreign key created objects.
to overcome this, need fetch objects created bulk_create
.
now question "assuming in situation, there no way uniquely identify created objects, how fetch them?"
currently maintaining time_stamp fetch them, below-
my_objects = [] # timestamp used fetching created objects time_stamp = datetime.datetime.now() # creating list of intantiated objects obj_data in obj_data_list: my_objects.append(mymodel(**obj_data)) # bulk inserting instantiated objects db mymodel.objects.bulk_create(my_objects) # using timestamp fetch created objects mymodel.objects.filter(created_at__gte=time_stamp)
now works good, fail in 1 case.
- if @ time of bulk-creating these objects, more objects created somewhere else, objects fetched in query, not desired.
can come better solution?
as bulk_create
not create primary keys, you'll have supply keys yourself.
this process simple if not using default generated primary key, autofield
.
if sticking default, you'll need wrap code atomic transaction , supply primary key yourself. way you'll know records inserted.
from django.db import transaction inserted_ids = [] transacation.atomic(): my_objects = [] max_id = int(mymodel.objects.latest('pk').pk) id_count = max_id obj_data in obj_data_list: id_count += 1 obj_data['id'] = id_count inserted_ids.append(obj_data['id']) my_objects.append(mymodel(**obj_data)) mymodel.objects.bulk_create(my_objects) inserted_ids = range(max_id, id_count)
Comments
Post a Comment