#9519: Add QuerySet.bulk_delete() that issues only a single SQL query -------------------------------------+------------------------------------- Reporter: Tarken | Owner: nobody Type: New feature | Status: new Component: Database layer | Version: SVN (models, ORM) | Resolution: Severity: Normal | Triage Stage: Accepted Keywords: database, queryset, | Needs documentation: 0 delete | Patch needs improvement: 0 Has patch: 0 | UI/UX: 0 Needs tests: 0 | Easy pickings: 0 | -------------------------------------+-------------------------------------
Comment (by kmike): Probably incomplete implementation/workaround that works for me: {{{ from django.db import transaction from django.db.models.sql.subqueries import DeleteQuery def truncate_queryset(qs): """ Deletes all records matched by queryset using DELETE from table WHERE <condition> query without fetching PK values for all items in original queryset. """ delete_query = qs.query.clone(DeleteQuery) # transaction management code is copied from QuerySet.update if not transaction.is_managed(using=qs.db): transaction.enter_transaction_management(using=qs.db) forced_managed = True else: forced_managed = False try: delete_query.get_compiler(qs.db).execute_sql(None) if forced_managed: transaction.commit(using=qs.db) else: transaction.commit_unless_managed(using=qs.db) finally: if forced_managed: transaction.leave_transaction_management(using=qs.db) }}} -- Ticket URL: <https://code.djangoproject.com/ticket/9519#comment:13> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.