update() and bulk_update() do not call save() or signals, and do not update auto_now fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # ====================================================================== # Q: Increase the price of all products in a category by 5%, and update 100,000 # products from a CSV. # # update() and bulk_update() do not call save() or signals, and do not update # auto_now fields. You need to say this. For new rows: bulk_create(..., # update_conflicts=True, unique_fields=['sku'], update_fields=['price']), which # does an upsert. # ====================================================================== # A single UPDATE in the database, without loading objects Product.objects.filter(category=cat).update(price=F('price') * Decimal('1.05')) # From CSV: bulk_update in batches by_sku = {row['sku']: Decimal(row['price']) for row in csv.DictReader(f)} products = list(Product.objects.filter(sku__in=by_sku).only('id', 'sku', 'price')) for p in products: p.price = by_sku[p.sku] Product.objects.bulk_update(products, ['price'], batch_size=1000) |