How to do django transaction to a set of code , including queries as well as local operations

permissionIds = [1,2...]

try:

with transaction.atomic():
 RolePermissionMap.objects.bulk_create([
    RolePermissionMap(role_id=role.id, permission_id=id) for id in permissionIds])
  
  rolePermissionsMap[roleId] = permissionIds
  # map should NOT update if the insert fails
  return Response({"ok": 1}, status=200)

except: return Response({"ok": 0}

In my code , I need to create multiple raws at a time, incase one of the given permission Id fails, it shouldnot do the next step , which storing some data in to a dict.(its not a db transaction, so it cannot rollback).

how can I do it efficiently?

Back to Top