1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #16891 -- Made Model/QuerySet.delete() return the number of deleted objects.

This commit is contained in:
Alexander Sosnovskiy
2015-03-07 11:56:25 +03:00
committed by Tim Graham
parent 9c8a2ab81d
commit 04e8d890ae
9 changed files with 120 additions and 20 deletions

View File

@@ -537,7 +537,8 @@ Deleting objects
Issues an SQL ``DELETE`` for the object. This only deletes the object in the
database; the Python instance will still exist and will still have data in
its fields.
its fields. This method returns the number of objects deleted and a dictionary
with the number of deletions per object type.
For more details, including how to delete objects in bulk, see
:ref:`topics-db-queries-delete`.
@@ -553,6 +554,10 @@ keep the parent model's data.
The ``keep_parents`` parameter was added.
.. versionchanged:: 1.9
The return value describing the number of objects deleted was added.
Pickling objects
================

View File

@@ -2070,8 +2070,11 @@ delete
.. method:: delete()
Performs an SQL delete query on all rows in the :class:`.QuerySet`. The
``delete()`` is applied instantly. You cannot call ``delete()`` on a
Performs an SQL delete query on all rows in the :class:`.QuerySet` and
returns the number of objects deleted and a dictionary with the number of
deletions per object type.
The ``delete()`` is applied instantly. You cannot call ``delete()`` on a
:class:`.QuerySet` that has had a slice taken or can otherwise no longer be
filtered.
@@ -2081,15 +2084,22 @@ For example, to delete all the entries in a particular blog::
# Delete all the entries belonging to this Blog.
>>> Entry.objects.filter(blog=b).delete()
(4, {'weblog.Entry': 2, 'weblog.Entry_authors': 2})
.. versionchanged:: 1.9
The return value describing the number of objects deleted was added.
By default, Django's :class:`~django.db.models.ForeignKey` emulates the SQL
constraint ``ON DELETE CASCADE`` — in other words, any objects with foreign
keys pointing at the objects to be deleted will be deleted along with them.
For example::
blogs = Blog.objects.all()
>>> blogs = Blog.objects.all()
# This will delete all Blogs and all of their Entry objects.
blogs.delete()
>>> blogs.delete()
(5, {'weblog.Blog': 1, 'weblog.Entry': 2, 'weblog.Entry_authors': 2})
This cascade behavior is customizable via the
:attr:`~django.db.models.ForeignKey.on_delete` argument to the