Ticket #12086: 12086.patch

File 12086.patch, 9.6 KB (added by Derek Willis, 15 years ago)

Added tests and docs to patch

  • django/contrib/sessions/tests.py

    From 61aadb890f7e7bb41cf2df35d25d32d6e8ae4429 Mon Sep 17 00:00:00 2001
    From: Derek Willis <dwillis@gmail.com>
    Date: Mon, 22 Mar 2010 22:01:15 -0400
    Subject: [PATCH] QuerySet.delete() returns number deleted
    
    ---
     django/contrib/sessions/tests.py                   |    3 +++
     django/db/models/query.py                          |    3 +++
     docs/topics/db/queries.txt                         |    6 ++++--
     tests/modeltests/basic/models.py                   |    1 +
     tests/modeltests/generic_relations/models.py       |    3 ++-
     tests/modeltests/many_to_many/models.py            |    2 ++
     tests/modeltests/many_to_one/models.py             |    1 +
     tests/modeltests/proxy_models/models.py            |    1 +
     .../custom_managers_regress/models.py              |    1 +
     .../regressiontests/m2m_through_regress/models.py  |    2 ++
     .../model_inheritance_regress/models.py            |    2 +-
     tests/regressiontests/model_regress/models.py      |    1 +
     12 files changed, 22 insertions(+), 4 deletions(-)
    
    diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py
    index f0a3c4e..6dbf676 100644
    a b True  
    5151# Submitting an invalid session key (either by guessing, or if the db has
    5252# removed the key) results in a new key being generated.
    5353>>> Session.objects.filter(pk=db_session.session_key).delete()
     541
    5455>>> db_session = DatabaseSession(db_session.session_key)
    5556>>> db_session.save()
    5657>>> DatabaseSession('1').get('cat')
    False  
    126127True
    127128
    128129>>> Session.objects.filter(pk=file_session.session_key).delete()
     1300
    129131>>> file_session = FileSession(file_session.session_key)
    130132>>> file_session.save()
    131133
    True  
    189191True
    190192
    191193>>> Session.objects.filter(pk=cache_session.session_key).delete()
     1940
    192195>>> cache_session = CacheSession(cache_session.session_key)
    193196>>> cache_session.save()
    194197>>> cache_session.delete(cache_session.session_key)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index f6b4419..c123cfd 100644
    a b class QuerySet(object):  
    428428        # Delete objects in chunks to prevent the list of related objects from
    429429        # becoming too long.
    430430        seen_objs = None
     431        count = 0
    431432        while 1:
    432433            # Collect all the objects to be deleted in this chunk, and all the
    433434            # objects that are related to the objects that are to be deleted.
    434435            seen_objs = CollectedObjects(seen_objs)
    435436            for object in del_query[:CHUNK_SIZE]:
    436437                object._collect_sub_objects(seen_objs)
     438                count += 1
    437439
    438440            if not seen_objs:
    439441                break
    class QuerySet(object):  
    441443
    442444        # Clear the result cache, in case this QuerySet gets reused.
    443445        self._result_cache = None
     446        return count
    444447    delete.alters_data = True
    445448
    446449    def update(self, **kwargs):
  • docs/topics/db/queries.txt

    diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt
    index 286a0c4..7389531 100644
    a b deletes the object and has no return value. Example::  
    729729    e.delete()
    730730
    731731You can also delete objects in bulk. Every ``QuerySet`` has a ``delete()``
    732 method, which deletes all members of that ``QuerySet``.
     732method, which deletes all members of that ``QuerySet`` and returns the number of
     733items deleted.
    733734
    734735For example, this deletes all ``Entry`` objects with a ``pub_date`` year of
    7357362005::
    736737
    737     Entry.objects.filter(pub_date__year=2005).delete()
     738    >>> Entry.objects.filter(pub_date__year=2005).delete()
     739    42
    738740
    739741Keep in mind that this will, whenever possible, be executed purely in
    740742SQL, and so the ``delete()`` methods of individual object instances
  • tests/modeltests/basic/models.py

    diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py
    index c86cb3a..f278757 100644
    a b AttributeError: Manager isn't accessible via Article instances  
    365365>>> Article.objects.all()
    366366[<Article: Area woman programs in Python>, <Article: Second article>, <Article: Third article>, <Article: Article 6>, <Article: Default headline>, <Article: Fourth article>, <Article: Article 7>, <Article: Updated article 8>]
    367367>>> Article.objects.filter(id__lte=4).delete()
     3684
    368369>>> Article.objects.all()
    369370[<Article: Article 6>, <Article: Default headline>, <Article: Article 7>, <Article: Updated article 8>]
    370371"""}
  • tests/modeltests/generic_relations/models.py

    diff --git a/tests/modeltests/generic_relations/models.py b/tests/modeltests/generic_relations/models.py
    index 1bc6577..075eab0 100644
    a b __test__ = {'API_TESTS':"""  
    167167[(u'clearish', <ContentType: mineral>, 1), (u'fatty', <ContentType: animal>, 1), (u'salty', <ContentType: vegetable>, 2), (u'shiny', <ContentType: animal>, 1)]
    168168
    169169>>> TaggedItem.objects.filter(tag='fatty').delete()
    170 
     1701
    171171>>> ctype = ContentType.objects.get_for_model(lion)
    172172>>> Animal.objects.filter(tags__content_type=ctype)
    173173[<Animal: Platypus>]
    __test__ = {'API_TESTS':"""  
    197197# Filtering and deleting works
    198198>>> subjective = ["cooler"]
    199199>>> tiger.comparisons.filter(comparative__in=subjective).delete()
     2002
    200201>>> Comparison.objects.all()
    201202[<Comparison: cheetah is faster than tiger>, <Comparison: tiger is stronger than cheetah>]
    202203
  • tests/modeltests/many_to_many/models.py

    diff --git a/tests/modeltests/many_to_many/models.py b/tests/modeltests/many_to_many/models.py
    index 21b4e82..a4a9c8d 100644
    a b TypeError: 'Publication' instance expected  
    243243
    244244# Bulk delete some Publications - references to deleted publications should go
    245245>>> Publication.objects.filter(title__startswith='Science').delete()
     2462
    246247>>> Publication.objects.all()
    247248[<Publication: Highlights for Children>, <Publication: The Python Journal>]
    248249>>> Article.objects.all()
    TypeError: 'Publication' instance expected  
    255256>>> print q
    256257[<Article: Django lets you build Web apps easily>]
    257258>>> q.delete()
     2591
    258260
    259261# After the delete, the QuerySet cache needs to be cleared, and the referenced objects should be gone
    260262>>> print q
  • tests/modeltests/many_to_one/models.py

    diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py
    index c6be981..a64dfd3 100644
    a b True  
    292292
    293293# You can delete using a JOIN in the query.
    294294>>> Reporter.objects.filter(article__headline__startswith='This').delete()
     2953
    295296>>> Reporter.objects.all()
    296297[]
    297298>>> Article.objects.all()
  • tests/modeltests/proxy_models/models.py

    diff --git a/tests/modeltests/proxy_models/models.py b/tests/modeltests/proxy_models/models.py
    index 28446b9..b2e4e8f 100644
    a b FieldError: Proxy model 'NoNewFields' contains model fields.  
    264264# Manager tests.
    265265
    266266>>> Person.objects.all().delete()
     2675
    267268>>> _ = Person.objects.create(name="fred")
    268269>>> _ = Person.objects.create(name="wilma")
    269270>>> _ = Person.objects.create(name="barney")
  • tests/regressiontests/custom_managers_regress/models.py

    diff --git a/tests/regressiontests/custom_managers_regress/models.py b/tests/regressiontests/custom_managers_regress/models.py
    index 898730e..e30eec1 100644
    a b instances. This is a regression test for #8990, #9527  
    5656Deleting related objects should also not be distracted by a restricted manager
    5757on the related object. This is a regression test for #2698.
    5858>>> RestrictedModel.plain_manager.all().delete()
     591
    5960>>> for name, public in (('one', True), ('two', False), ('three', False)):
    6061...     _ = RestrictedModel.objects.create(name=name, is_public=public, related=related)
    6162
  • tests/regressiontests/m2m_through_regress/models.py

    diff --git a/tests/regressiontests/m2m_through_regress/models.py b/tests/regressiontests/m2m_through_regress/models.py
    index 56aecd6..0c25775 100644
    a b AttributeError: Cannot use create() on a ManyToManyField which specifies an inte  
    126126# one for each end of the m2m, plus the through model.
    127127
    128128>>> User.objects.all().delete()
     1293
    129130>>> UserMembership.objects.all().delete()
     1310
    130132>>> frank.delete()
    131133>>> rock.delete()
    132134>>> jim.delete()
  • tests/regressiontests/model_inheritance_regress/models.py

    diff --git a/tests/regressiontests/model_inheritance_regress/models.py b/tests/regressiontests/model_inheritance_regress/models.py
    index c669b23..a2f3f1b 100644
    a b True  
    239239
    240240# This should delete both Restuarants, plus the related places, plus the ItalianRestaurant.
    241241>>> Restaurant.objects.all().delete()
    242 
     2422
    243243>>> Place.objects.get(pk=ident)
    244244Traceback (most recent call last):
    245245...
  • tests/regressiontests/model_regress/models.py

    diff --git a/tests/regressiontests/model_regress/models.py b/tests/regressiontests/model_regress/models.py
    index 420f2c2..2947841 100644
    a b if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] not in (  
    159159# The idea is that all these creations and saving should work without crashing.
    160160# It's not rocket science.
    161161>>> Article.objects.all().delete()
     1622
    162163>>> dt1 = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(600))
    163164>>> dt2 = datetime.datetime(2008, 8, 31, 17, 20, tzinfo=tzinfo.FixedOffset(600))
    164165>>> obj = Article.objects.create(headline="A headline", pub_date=dt1, article_text="foo")
Back to Top