def _value_for_db(self, value, field, field_kind, db_type, lookup): """ GAE database may store a restricted set of Python types, for some cases it has its own types like Key, Text or Blob. TODO: Consider moving empty list handling here (from insert). """ # Store Nones as Nones to handle nullable fields, even keys. if value is None: return None # Parent can handle iterable fields and Django wrappers. value = super(DatabaseOperations, self)._value_for_db( value, field, field_kind, db_type, lookup) # Convert decimals to strings preserving order. if field_kind == 'DecimalField': value = decimal_to_string( value, field.max_digits, field.decimal_places) # Create GAE db.Keys from Django keys. # We use model's table name as key kind (the table of the model # of the instance that the key identifies, for ForeignKeys and # other relations). if db_type == 'key': # value = self._value_for_db_key(value, field_kind) if isinstance(value, str) and len(value) > 500: warnings.warn(u"GAE string key '%s' exceeded maximum_length of 500 and was truncated." % value, RuntimeWarning) value = value[:500] try: if not isinstance(value, (Key, AncestorKey)): value = key_from_path(field.model._meta.db_table, value) except (BadArgumentError, BadValueError,): raise DatabaseError("Only strings and positive integers " "may be used as keys on GAE.") # Store all strings as unicode, use db.Text for longer content. elif db_type == 'string' or db_type == 'text': if isinstance(value, str): value = value.decode('utf-8') if db_type == 'text': value = Text(value) # Store all date / time values as datetimes, by using some # default time or date. elif db_type == 'date': value = datetime.datetime.combine(value, self.DEFAULT_TIME) elif db_type == 'time': value = datetime.datetime.combine(self.DEFAULT_DATE, value) # Store BlobField, DictField and EmbeddedModelField values as Blobs. elif db_type == 'bytes': value = Blob(value) return value
def _value_for_db(self, value, field, field_kind, db_type, lookup): """ GAE database may store a restricted set of Python types, for some cases it has its own types like Key, Text or Blob. TODO: Consider moving empty list handling here (from insert). """ # Store Nones as Nones to handle nullable fields, even keys. if value is None: return None # Parent can handle iterable fields and Django wrappers. value = super(DatabaseOperations, self)._value_for_db(value, field, field_kind, db_type, lookup) # Convert decimals to strings preserving order. if field_kind == 'DecimalField': value = decimal_to_string(value, field.max_digits, field.decimal_places) # Create GAE db.Keys from Django keys. # We use model's table name as key kind (the table of the model # of the instance that the key identifies, for ForeignKeys and # other relations). if db_type == 'key': # value = self._value_for_db_key(value, field_kind) try: value = key_from_path(field.model._meta.db_table, value) except ( BadArgumentError, BadValueError, ): raise DatabaseError("Only strings and positive integers " "may be used as keys on GAE.") # Store all strings as unicode, use db.Text for longer content. elif db_type == 'string' or db_type == 'text': if isinstance(value, str): value = value.decode('utf-8') if db_type == 'text': value = Text(value) # Store all date / time values as datetimes, by using some # default time or date. elif db_type == 'date': value = datetime.datetime.combine(value, self.DEFAULT_TIME) elif db_type == 'time': value = datetime.datetime.combine(self.DEFAULT_DATE, value) # Store BlobField, DictField and EmbeddedModelField values as Blobs. elif db_type == 'bytes': value = Blob(value) return value
def _value_for_db(self, value, field, field_kind, db_type, lookup): """ Allows parent to handle nonrel fields, convert AutoField keys to ObjectIds and date and times to datetimes. Let everything else pass to PyMongo -- when the value is used the driver will raise an exception if it got anything unacceptable. """ if value is None: return None # Parent can handle iterable fields and Django wrappers. value = super(DatabaseOperations, self)._value_for_db(value, field, field_kind, db_type, lookup) # Convert decimals to strings preserving order. if field_kind == 'DecimalField': value = decimal_to_string(value, field.max_digits, field.decimal_places) # Anything with the "key" db_type is converted to an ObjectId. if db_type == 'key': try: return ObjectId(value) # Provide a better message for invalid IDs. except InvalidId: assert isinstance(value, unicode) if len(value) > 13: value = value[:10] + '...' msg = "AutoField (default primary key) values must be " \ "strings representing an ObjectId on MongoDB (got " \ "%r instead)." % value if field.model._meta.db_table == 'django_site': # Also provide some useful tips for (very common) issues # with settings.SITE_ID. msg += " Please make sure your SITE_ID contains a " \ "valid ObjectId string." raise DatabaseError(msg) # PyMongo can only process datatimes? elif db_type == 'date': return datetime.datetime(value.year, value.month, value.day) elif db_type == 'time': return datetime.datetime(1, 1, 1, value.hour, value.minute, value.second, value.microsecond) return value
def _value_for_db(self, value, field, field_kind, db_type, lookup): """ Allows parent to handle nonrel fields, convert AutoField keys to ObjectIds and date and times to datetimes. Let everything else pass to PyMongo -- when the value is used the driver will raise an exception if it got anything unacceptable. """ if value is None: return None # Parent can handle iterable fields and Django wrappers. value = super(DatabaseOperations, self)._value_for_db( value, field, field_kind, db_type, lookup) # Convert decimals to strings preserving order. if field_kind == 'DecimalField': value = decimal_to_string( value, field.max_digits, field.decimal_places) # Anything with the "key" db_type is converted to an ObjectId. if db_type == 'key': try: return ObjectId(value) # Provide a better message for invalid IDs. except InvalidId: assert isinstance(value, (str, unicode)) if len(value) > 13: value = value[:10] + '...' msg = "AutoField (default primary key) values must be " \ "strings representing an ObjectId on MongoDB (got " \ "%r instead)." % value if field.model._meta.db_table == 'django_site': # Also provide some useful tips for (very common) issues # with settings.SITE_ID. msg += " Please make sure your SITE_ID contains a " \ "valid ObjectId string." raise DatabaseError(msg) # PyMongo can only process datatimes? elif db_type == 'date': return datetime.datetime(value.year, value.month, value.day) elif db_type == 'time': return datetime.datetime(1, 1, 1, value.hour, value.minute, value.second, value.microsecond) return value