# Ran test create_v2 in 0.477 seconds def create_v1(hotels): for hotel in hotels: hotel.save() def create_v2(hotels): Hotel.objects.bulk_create(hotels)
hotel.save() def set_new_manager_v2(hotel, manager): # Call save(update_fields) when you have full control # over the fields that change. hotel.manager = manager hotel.save(update_fields=['manager'])
queryset.update() to avoid triggering # save signals. It will only run an SQL UPDATE. lookup(pk=hotel.pk).update(manager=manager) lookup = Hotel.objects.filter for hotel, manager in hotels_and_managers: set_new_manager_v4(lookup, hotel, manager)
in new_data.items(): setattr(manager, field, value) # Not recommended as manager points to a third- # party model which might override the data # that gets written # manager.save(update_fields=list(new_data.keys())) manager.save()
in 0.175117969513 # Ran test print_hotel_names_v2 in 0.173953056335 def print_hotel_names_v1(): for hotel in Hotel.objects.all(): print hotel.name def print_hotel_names_v2(): for hotel in Hotel.objects.only('name'): print hotel.name
in 0.075676202774 # Ran test print_hotel_names_v4 in 0.0628550052643 def print_hotel_names_v3(): for data in Hotel.objects.values('name'): print data['name'] def print_hotel_names_v4(): for name in Hotel.objects.values_list('name', flat=True): print name