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

View file

@ -214,6 +214,10 @@ Models
<django.db.models.Model.delete>` to allow deleting only a child's data in a
model that uses multi-table inheritance.
* :meth:`Model.delete() <django.db.models.Model.delete>`
and :meth:`QuerySet.delete() <django.db.models.query.QuerySet.delete>` return
the number of objects deleted.
* Added a system check to prevent defining both ``Meta.ordering`` and
``order_with_respect_to`` on the same model.

View file

@ -899,9 +899,15 @@ Deleting objects
The delete method, conveniently, is named
:meth:`~django.db.models.Model.delete`. This method immediately deletes the
object and has no return value. Example::
object and returns the number of objects deleted and a dictionary with
the number of deletions per object type. Example::
e.delete()
>>> e.delete()
(1, {'weblog.Entry': 1})
.. versionchanged:: 1.9
The return value describing the number of objects deleted was added.
You can also delete objects in bulk. Every
:class:`~django.db.models.query.QuerySet` has a
@ -911,7 +917,8 @@ members of that :class:`~django.db.models.query.QuerySet`.
For example, this deletes all ``Entry`` objects with a ``pub_date`` year of
2005::
Entry.objects.filter(pub_date__year=2005).delete()
>>> Entry.objects.filter(pub_date__year=2005).delete()
(5, {'webapp.Entry': 5})
Keep in mind that this will, whenever possible, be executed purely in SQL, and
so the ``delete()`` methods of individual object instances will not necessarily
@ -923,6 +930,10 @@ object individually) rather than using the bulk
:meth:`~django.db.models.query.QuerySet.delete` method of a
:class:`~django.db.models.query.QuerySet`.
.. versionchanged:: 1.9
The return value describing the number of objects deleted was added.
When Django deletes an object, by default it emulates the behavior of the SQL
constraint ``ON DELETE CASCADE`` -- in other words, any objects which had
foreign keys pointing at the object to be deleted will be deleted along with