Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[soc2010/query-refactor] Implemented deletion. Thanks to Josh Ourisma…

…n for pointing this out.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13433 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 485bfe486148783870568ba7e47d41d8ab794069 1 parent 59089a7
Alex Gaynor alex authored
6 django/contrib/mongodb/compiler.py
@@ -176,3 +176,9 @@ def update(self, result_type):
176 176 vals,
177 177 multi=True
178 178 )
  179 +
  180 +
  181 +class SQLDeleteCompiler(SQLCompiler):
  182 + def delete(self, result_type):
  183 + filters = self.get_filters(self.query.where)
  184 + self.connection.db[self.query.model._meta.db_table].remove(filters)
3  django/db/models/sql/compiler.py
@@ -821,6 +821,9 @@ def as_sql(self):
821 821 where, params = self.query.where.as_sql(qn=qn, connection=self.connection)
822 822 result.append('WHERE %s' % where)
823 823 return ' '.join(result), tuple(params)
  824 +
  825 + def delete(self, *args, **kwargs):
  826 + return self.execute_sql(*args, **kwargs)
824 827
825 828 class SQLUpdateCompiler(SQLCompiler):
826 829 def as_sql(self):
4 django/db/models/sql/subqueries.py
@@ -24,7 +24,7 @@ class DeleteQuery(Query):
24 24 def do_query(self, table, where, using):
25 25 self.tables = [table]
26 26 self.where = where
27   - self.get_compiler(using).execute_sql(None)
  27 + self.get_compiler(using).delete(None)
28 28
29 29 def delete_batch(self, pk_list, using):
30 30 """
@@ -36,7 +36,7 @@ def delete_batch(self, pk_list, using):
36 36 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
37 37 where = self.where_class()
38 38 field = self.model._meta.pk
39   - where.add((Constraint(None, field.column, field), 'in',
  39 + where.add((Constraint(self.model._meta.db_table, field.column, field), 'in',
40 40 pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), AND)
41 41 self.do_query(self.model._meta.db_table, where, using=using)
42 42
16 tests/regressiontests/mongodb/tests.py
@@ -50,6 +50,22 @@ def test_f_expressions(self):
50 50 Artist.objects.filter(pk=k.pk).update(age=1 + F("age"))
51 51 self.assertEqual(Artist.objects.get(pk=k.pk).age, 58)
52 52
  53 + def test_delete(self):
  54 + o = Artist.objects.create(name="O.A.R.", good=True)
  55 + self.assertEqual(Artist.objects.count(), 1)
  56 +
  57 + o.delete()
  58 + self.assertEqual(Artist.objects.count(), 0)
  59 +
  60 + def test_bulk_delete(self):
  61 + d = Artist.objects.create(name="Dispatch", good=True)
  62 + b = Artist.objects.create(name="Backstreet Boys", good=False)
  63 +
  64 + # Good riddance.
  65 + Artist.objects.filter(good=False).delete()
  66 + self.assertEqual(Artist.objects.count(), 1)
  67 + self.assertEqual(Artist.objects.get(), d)
  68 +
53 69 def test_count(self):
54 70 Artist.objects.create(name="Billy Joel", good=True)
55 71 Artist.objects.create(name="John Mellencamp", good=True)

0 comments on commit 485bfe4

Please sign in to comment.
Something went wrong with that request. Please try again.