Skip to content
Browse files

[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...
1 parent 59089a7 commit 485bfe486148783870568ba7e47d41d8ab794069 @alex alex committed Jul 17, 2010
View
6 django/contrib/mongodb/compiler.py
@@ -176,3 +176,9 @@ def update(self, result_type):
vals,
multi=True
)
+
+
+class SQLDeleteCompiler(SQLCompiler):
+ def delete(self, result_type):
+ filters = self.get_filters(self.query.where)
+ self.connection.db[self.query.model._meta.db_table].remove(filters)
View
3 django/db/models/sql/compiler.py
@@ -821,6 +821,9 @@ def as_sql(self):
where, params = self.query.where.as_sql(qn=qn, connection=self.connection)
result.append('WHERE %s' % where)
return ' '.join(result), tuple(params)
+
+ def delete(self, *args, **kwargs):
+ return self.execute_sql(*args, **kwargs)
class SQLUpdateCompiler(SQLCompiler):
def as_sql(self):
View
4 django/db/models/sql/subqueries.py
@@ -24,7 +24,7 @@ class DeleteQuery(Query):
def do_query(self, table, where, using):
self.tables = [table]
self.where = where
- self.get_compiler(using).execute_sql(None)
+ self.get_compiler(using).delete(None)
def delete_batch(self, pk_list, using):
"""
@@ -36,7 +36,7 @@ def delete_batch(self, pk_list, using):
for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
where = self.where_class()
field = self.model._meta.pk
- where.add((Constraint(None, field.column, field), 'in',
+ where.add((Constraint(self.model._meta.db_table, field.column, field), 'in',
pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), AND)
self.do_query(self.model._meta.db_table, where, using=using)
View
16 tests/regressiontests/mongodb/tests.py
@@ -50,6 +50,22 @@ def test_f_expressions(self):
Artist.objects.filter(pk=k.pk).update(age=1 + F("age"))
self.assertEqual(Artist.objects.get(pk=k.pk).age, 58)
+ def test_delete(self):
+ o = Artist.objects.create(name="O.A.R.", good=True)
+ self.assertEqual(Artist.objects.count(), 1)
+
+ o.delete()
+ self.assertEqual(Artist.objects.count(), 0)
+
+ def test_bulk_delete(self):
+ d = Artist.objects.create(name="Dispatch", good=True)
+ b = Artist.objects.create(name="Backstreet Boys", good=False)
+
+ # Good riddance.
+ Artist.objects.filter(good=False).delete()
+ self.assertEqual(Artist.objects.count(), 1)
+ self.assertEqual(Artist.objects.get(), d)
+
def test_count(self):
Artist.objects.create(name="Billy Joel", good=True)
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.