def prep_lookup_key(self, model, value, field): if isinstance(value, basestring): value = value[:500] left = value[500:] if left: warnings.warn("Truncating primary key that is over 500 characters. " "THIS IS AN ERROR IN YOUR PROGRAM.", RuntimeWarning) # This is a bit of a hack. Basically when you query an integer PK with a # string containing an int. SQL seems to return the row regardless of type, and as far as # I can tell, Django at no point tries to cast the value to an integer. So, in the # case where the internal type is an AutoField, we try to cast the string value # I would love a more generic solution... patches welcome! # It would be nice to see the SQL output of the lookup_int_as_str test is on SQL, if # the string is converted to an int, I'd love to know where! if field.get_internal_type() == 'AutoField': try: value = int(value) except (TypeError, ValueError): pass value = get_datastore_key(model, value) else: value = get_datastore_key(model, value) return value
def prep_lookup_key(self, model, value, field): if isinstance(value, basestring): value = value[:500] left = value[500:] if left: warnings.warn( "Truncating primary key that is over 500 characters. " "THIS IS AN ERROR IN YOUR PROGRAM.", RuntimeWarning) # This is a bit of a hack. Basically when you query an integer PK with a # string containing an int. SQL seems to return the row regardless of type, and as far as # I can tell, Django at no point tries to cast the value to an integer. So, in the # case where the internal type is an AutoField, we try to cast the string value # I would love a more generic solution... patches welcome! # It would be nice to see the SQL output of the lookup_int_as_str test is on SQL, if # the string is converted to an int, I'd love to know where! if field.get_internal_type() == 'AutoField': try: value = int(value) except (TypeError, ValueError): pass value = get_datastore_key(model, value) else: value = get_datastore_key(model, value) return value
def prep_lookup_key(self, model, value, field): if isinstance(value, basestring): value = value[:500] left = value[500:] if left: warnings.warn( "Truncating primary key that is over 500 characters. " "THIS IS AN ERROR IN YOUR PROGRAM.", RuntimeWarning) value = get_datastore_key(model, value) else: value = get_datastore_key(model, value) return value
def prep_lookup_key(self, model, value, field): if isinstance(value, basestring): value = value[:500] left = value[500:] if left: warnings.warn("Truncating primary key that is over 500 characters. " "THIS IS AN ERROR IN YOUR PROGRAM.", RuntimeWarning) value = get_datastore_key(model, value) else: value = get_datastore_key(model, value) return value
def process_and_branch(query, and_branch): for child in and_branch[-1]: column, op, value = child[1] # for column, op, value in and_branch[-1]: if column == self.pk_col: column = "__key__" #FIXME: This EmptyResultSet check should happen during normalization so that Django doesn't count it as a query if op == "=" and "__key__ =" in query and query["__key__ ="] != value: # We've already done an exact lookup on a key, this query can't return anything! raise EmptyResultSet() if not isinstance(value, datastore.Key): value = get_datastore_key(self.model, value) key = "%s %s" % (column, op) try: if isinstance(value, basestring): value = coerce_unicode(value) if key in query: if type(query[key]) == list: if value not in query[key]: query[key].append(value) else: if query[key] != value: query[key] = [ query[key], value ] else: query[key] = value except datastore_errors.BadFilterError as e: raise NotSupportedError(str(e))
def __init__(self, connection, model, objs, fields, raw): self.has_pk = any([x.primary_key for x in fields]) self.entities = [] self.included_keys = [] self.model = model for obj in objs: if self.has_pk: # We must convert the PK value here, even though this normally happens in django_instance_to_entity otherwise # custom PK fields don't work properly value = model._meta.pk.get_db_prep_save( model._meta.pk.pre_save(obj, True), connection) self.included_keys.append( get_datastore_key(model, value) if value else None) if not self.model._meta.pk.blank and self.included_keys[ -1] is None: raise IntegrityError( "You must specify a primary key value for {} instances" .format(model)) else: # We zip() self.entities and self.included_keys in execute(), so they should be the same length self.included_keys.append(None) self.entities.append( django_instance_to_entity(connection, model, fields, raw, obj))
def __init__(self, connection, model, objs, fields, raw): self.has_pk = any(x.primary_key for x in fields) self.model = model self.objs = objs self.connection = connection self.namespace = connection.ops.connection.settings_dict.get( "NAMESPACE") self.raw = raw self.fields = fields self.entities = [] self.included_keys = [] for obj in self.objs: if self.has_pk: # We must convert the PK value here, even though this normally happens in django_instance_to_entities otherwise # custom PK fields don't work properly value = self.model._meta.pk.get_db_prep_save( self.model._meta.pk.pre_save(obj, True), self.connection) self.included_keys.append( get_datastore_key(self.model, value, self.namespace ) if value else None) if value == 0: raise IntegrityError( "The datastore doesn't support 0 as a key value") if not self.model._meta.pk.blank and self.included_keys[ -1] is None: raise IntegrityError( "You must specify a primary key value for {} instances" .format(self.model)) else: # We zip() self.entities and self.included_keys in execute(), so they should be the same length self.included_keys.append(None) # We don't use the values returned, but this does make sure we're # doing the same validation as Django. See issue #493 for an # example of how not doing this can mess things up for field in fields: field.get_db_prep_save( getattr(obj, field.attname) if raw else field.pre_save( obj, True), connection=connection, ) primary, descendents = django_instance_to_entities( self.connection, self.fields, self.raw, obj) # Append the entity, and any descendents to the list to insert self.entities.append((primary, descendents))
def __init__(self, connection, model, objs, fields, raw): self.has_pk = any(x.primary_key for x in fields) self.model = model self.objs = objs self.connection = connection self.namespace = connection.ops.connection.settings_dict.get("NAMESPACE") self.raw = raw self.fields = fields self.entities = [] self.included_keys = [] for obj in self.objs: if self.has_pk: # We must convert the PK value here, even though this normally happens in # django_instance_to_entities otherwise # custom PK fields don't work properly value = self.model._meta.pk.get_db_prep_save( self.model._meta.pk.pre_save(obj, True), self.connection ) self.included_keys.append( get_datastore_key(self.model, value, self.namespace) if value else None ) if value == 0: raise IntegrityError("The datastore doesn't support 0 as a key value") if not self.model._meta.pk.blank and self.included_keys[-1] is None: raise IntegrityError("You must specify a primary key value for {} instances".format(self.model)) else: # We zip() self.entities and self.included_keys in execute(), so they should be the same length self.included_keys.append(None) # We don't use the values returned, but this does make sure we're # doing the same validation as Django. See issue #493 for an # example of how not doing this can mess things up for field in fields: field.get_db_prep_save( getattr(obj, field.attname) if raw else field.pre_save(obj, True), connection=connection, ) primary, descendents = django_instance_to_entities( self.connection, self.fields, self.raw, obj ) # Append the entity, and any descendents to the list to insert self.entities.append((primary, descendents))
def __init__(self, connection, model, objs, fields, raw): self.has_pk = any([x.primary_key for x in fields]) self.entities = [] self.included_keys = [] self.model = model for obj in objs: if self.has_pk: self.included_keys.append(get_datastore_key(model, obj.pk)) else: #We zip() self.entities and self.included_keys in execute(), so they should be the same legnth self.included_keys.append(None) self.entities.append( django_instance_to_entity(connection, model, fields, raw, obj) )
def __init__(self, connection, model, objs, fields, raw): self.has_pk = any([x.primary_key for x in fields]) self.entities = [] self.included_keys = [] self.model = model for obj in objs: if self.has_pk: #FIXME: Apparently if the PK is required, and obj.pk is None here, we need to raise an IntegrityError self.included_keys.append(get_datastore_key(model, obj.pk) if obj.pk else None) else: #We zip() self.entities and self.included_keys in execute(), so they should be the same legnth self.included_keys.append(None) self.entities.append( django_instance_to_entity(connection, model, fields, raw, obj) )
def process_and_branch(query, and_branch): for column, op, value in and_branch[-1]: if column == self.pk_col: column = "__key__" #FIXME: This EmptyResultSet check should happen during normalization so that Django doesn't count it as a query if op == "=" and "__key__ =" in query: #We've already done an exact lookup on a key, this query can't return anything! raise EmptyResultSet() if not isinstance(value, datastore.Key): value = get_datastore_key(self.model, value) key = "%s %s" % (column, op) if key in query: query[key] = [ query[key], value ] else: query[key] = value
def __init__(self, connection, model, objs, fields, raw): self.has_pk = any([x.primary_key for x in fields]) self.entities = [] self.included_keys = [] self.model = model for obj in objs: if self.has_pk: # We must convert the PK value here, even though this normally happens in django_instance_to_entity otherwise # custom PK fields don't work properly value = model._meta.pk.get_db_prep_save(model._meta.pk.pre_save(obj, True), connection) self.included_keys.append(get_datastore_key(model, value) if value else None) if not self.model._meta.pk.blank and self.included_keys[-1] is None: raise IntegrityError("You must specify a primary key value for {} instances".format(model)) else: # We zip() self.entities and self.included_keys in execute(), so they should be the same length self.included_keys.append(None) self.entities.append(django_instance_to_entity(connection, model, fields, raw, obj))