Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Django import
- from django.db import transaction
- from django.db.models import Min, Max
- #Proyect imports
- from core.models import Cert
- def test_for_update():
- #We retrieve certs to work with them
- certs = Cert.objects.all().order_by('pk')
- certs = certs.select_for_update()# Thats the magic!
- total_rows = certs.count()
- print('{0} rows in total'.format(total_rows))
- #get batch size
- batch_size = int(input('Define Batch Size: '))
- print('It should need {0} selects to finish.'.format(total_rows/batch_size))
- with transaction.atomic():
- full = True
- while full:
- #Select for update
- batch = certs[batch_size]
- #We collect data to show:
- data = batch.aggregate(Min('pk'), Max('pk'))
- batch_count = batch.count()
- #We print
- print('{0} to {1} with {2} rows...'.format(
- data['pk__min'],
- data['pk__max'],
- batch_count
- ))
- #condition to exit:
- if batch_count < batch_size:
- full = False
- print('Finished process, success!')
Add Comment
Please, Sign In to add comment