Commits

Alex Burgel committed 810ca31

fix issues with foreignkeys

  • Participants
  • Parent commits a51f2af

Comments (0)

Files changed (3)

             value = Blob(pickle.dumps(value))
 
         if db_type == 'gae_key':
-            return value
+            if isinstance(value, GAEKey) and value.has_real_key():
+                return value.real_key
+            else:
+                return value
         elif db_type == 'longtext':
             # long text fields cannot be indexed on GAE so use GAE's database
             # type Text
         for column, value in data.items():
             if column == opts.pk.column:
                 if isinstance(value, GAEKey):
-                    if value.parent_key:
+                    if value.parent_key and value.parent_key.has_real_key():
                         kwds['parent'] = value.parent_key.real_key
                     if isinstance(value.id_or_name, basestring):
                         kwds['name'] = value.id_or_name
                     elif value.id_or_name is not None:
                         kwds['id'] = value.id_or_name
+                elif isinstance(value, Key):
+                    kwds['parent'] = value.parent()
+                    if value.name():
+                        kwds['name'] = value.name()
+                    elif value.id():
+                        kwds['id'] = value.id()
                 elif isinstance(value, basestring):
                     kwds['name'] = value
                 else:
         entity = Entity(opts.db_table, **kwds)
         entity.update(gae_data)
         key = Put(entity)
-        
+
         if not isinstance(opts.pk, GAEKeyField):
-            if key.id() is not None:
-                key = key.id()
-            else:
-                key = key.name()
-        
+            key = key.id_or_name()
         return key
 
 class SQLUpdateCompiler(NonrelUpdateCompiler, SQLCompiler):
             setattr(cls, self.parent_key_attname, property(get_parent_key, set_parent_key))
 
     def to_python(self, value):
+        if value is None:
+            return None
         if isinstance(value, GAEKey):
             return value
         if isinstance(value, Key):
             return GAEKey(real_key=value)
-        if value is None or len(value) < 1:
-            return None
+        if isinstance(value, basestring):
+            return GAEKey(real_key=Key(encoded=value))
         return GAEKey(id_or_name=value)
 
     def get_prep_value(self, value):
 
 class GAEAncestorKey(object):
     def __init__(self, key):
-        if key is None:
-            raise ValueError('key must not be None')
         if not isinstance(key, Key):
             raise ValueError('key must be of type Key')
 
     id_or_name = property(_get_id_or_name)
 
     def _get_parent_key(self):
-        if self._parent_key is None:
-            raise ValueError("This is a root entity, there is no parent.")
         return self._parent_key
     parent_key = property(_get_parent_key)
 
         return self._real_key
     real_key = property(_get_real_key)
 
+    def has_real_key(self):
+        return self._real_key is not None
+
     def as_ancestor(self):
         return GAEAncestorKey(self._get_real_key())
 
         return hash(self._real_key)
 
     def __str__(self):
-        return "<%s %s %s>" % (self._id_or_name, self._parent_key, self._real_key)
+        return str(self._real_key)