Author: Alex
Date: 2010-06-09 11:23:26 -0500 (Wed, 09 Jun 2010)
New Revision: 13340
Modified:
django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
Log:
[soc2010/query-refactor] MongoDB backend can now update saved objects.
Modified: django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
===================================================================
--- django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
2010-06-09 15:47:23 UTC (rev 13339)
+++ django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
2010-06-09 16:23:26 UTC (rev 13340)
@@ -9,6 +9,7 @@
class DatabaseFeatures(object):
interprets_empty_strings_as_nulls = False
+ typed_columns = False
class DatabaseOperations(object):
Modified:
django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
===================================================================
--- django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
2010-06-09 15:47:23 UTC (rev 13339)
+++ django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
2010-06-09 16:23:26 UTC (rev 13340)
@@ -4,6 +4,54 @@
self.query = query
self.connection = connection
self.using = using
+
+ def get_filters(self, where):
+ assert where.connector == "AND"
+ assert not where.negated
+ filters = {}
+ for child in where.children:
+ if isinstance(child, self.query.where_class):
+ # TODO: probably needs to check for dupe keys
+ filters.update(self.get_filters(child))
+ else:
+ field, val = self.make_atom(*child)
+ filters[field] = val
+ return filters
+
+ def make_atom(self, lhs, lookup_type, value_annotation, params_or_value):
+ assert lookup_type == "exact"
+ if hasattr(lhs, "process"):
+ lhs, params = lhs.process(lookup_type, params_or_value,
self.connection)
+ else:
+ params = Field().get_db_prep_lookup(lookup_type, params_or_value,
+ connection=self.connection, prepared=True)
+ assert isinstance(lhs, (list, tuple))
+ table, column, _ = lhs
+ assert table == self.query.model._meta.db_table
+ if column == self.query.model._meta.pk.column:
+ column = "_id"
+ return column, params[0]
+
+ def build_query(self):
+ assert not self.query.aggregates
+ assert len(self.query.alias_map) == 1
+ assert self.query.default_cols
+ assert not self.query.distinct
+ assert not self.query.extra
+ assert not self.query.having
+ assert self.query.high_mark is None
+ assert not self.query.order_by
+
+ filters = self.get_filters(self.query.where)
+ return
self.connection.db[self.query.model._meta.db_table].find(filters)
+
+ def has_results(self):
+ try:
+ self.build_query()[0]
+ except IndexError:
+ return False
+ else:
+ return True
class SQLInsertCompiler(SQLCompiler):
Modified:
django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
===================================================================
--- django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
2010-06-09 15:47:23 UTC (rev 13339)
+++ django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
2010-06-09 16:23:26 UTC (rev 13340)
@@ -215,6 +215,8 @@
# mapped to one of the built-in Django field types. In this case, you
# can implement db_type() instead of get_internal_type() to specify
# exactly which wacky database column type you want to use.
+ if not getattr(connection.features, "typed_columns", True):
+ return None
data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
try:
return connection.creation.data_types[self.get_internal_type()] %
data
Modified:
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
===================================================================
---
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
2010-06-09 15:47:23 UTC (rev 13339)
+++
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
2010-06-09 16:23:26 UTC (rev 13340)
@@ -9,3 +9,12 @@
self.assertTrue(b.pk is not None)
self.assertEqual(b.name, "Bruce Springsteen")
self.assertTrue(b.good)
+
+ def test_update(self):
+ l = Artist.objects.create(name="Lady Gaga", good=True)
+ self.assertTrue(l.pk is not None)
+ pk = l.pk
+ # Whoops, we screwed up.
+ l.good = False
+ l.save()
+ self.assertEqual(l.pk, pk)
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.