1👍
If what you’re trying to do is create instances of A alongside the B, then no bulk_create()
won’t do this. However, if the instances of A already exist in the database then you could manually add their pk’s to the list you pass to bulk_create()
. Then the B instances will be created with the correct relations to A.
a_list = [(A(user_id=user_id, **kwargs))]
A.objects.bulk_create(a_list)
(make b_list like the line bellow)
b_list = [(B(a__id=...,user_id=user_id, **kwargs))]
B.objects.bulk_create(b_list)
Noted: All of this can be done inside a single transaction.atomic
context.
Source:stackexchange.com