热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将djangotoolboxListField保存到GAE数据存储区时出错-ErrorwhilesavingdjangotoolboxListFieldtoGAEdatastore

WhilesavingListFieldtoGAEdatastore,IgetUnsupportedtypeforpropertytags:GAEdatastore

While saving ListField to GAE datastore, I get 'Unsupported type for property tags: '
GAE datastore receives a generator object to save into database:

在将ListField保存到GAE数据存储区时,我得到'属性标记的不支持类型:'GAE数据存储区接收一个生成器对象以保存到数据库中:

name: 'tags'
value:  at 0x049C6620>

dbindexer\compiler.py or djangotoolbox\db\basecompiler.py is perhaps generating this object from list object passed to it.

dbindexer \ compiler.py或djangotoolbox \ db \ basecompiler.py可能是从传递给它的列表对象生成此对象。

(, [1L])]

Any hints about possible problems?

有关可能出现问题的任何提示?

from django.db import models
from djangotoolbox.fields import ListField

class Tag(models.Model):
    name = models.CharField(max_length=255)

class Note(models.Model):
    tags = ListField(db.ForeignKey(Tag))

Exception:

Django Version: 1.3.1
Exception Type: DatabaseError
Exception Value: Unsupported type for property tags: 
Exception Location: google_appengine\google\appengine\api\datastore_types.py in ValidateProperty, line 1529

File "myapp\django\db\models\base.py" in save
  462. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "myapp\django\db\models\base.py" in save_base
  573. result = manager._insert(values, return_id=update_pk, using=using)
File "myapp\django\db\models\manager.py" in _insert
  195. return insert_query(self.model, values, **kwargs)
File "myapp\django\db\models\query.py" in insert_query
  1438. return query.get_compiler(using=using).execute_sql(return_id)
File "myapp\dbindexer\compiler.py" in execute_sql
  38.  return super(SQLInsertCompiler, self).execute_sql(return_id=return_id)
File "myapp\djangotoolbox\db\basecompiler.py" in execute_sql
  549. key = self.insert(field_values, return_id=return_id)
File "myapp\djangoappengine\db\compiler.py" in _func
  68.   return func(*args, **kwargs)
File "myapp\djangoappengine\db\compiler.py" in insert
  387.  entity.update(properties)
File "google_appengine\google\appengine\api\datastore.py" in update
  890.  self.__setitem__(name, value)
File "google_appengine\google\appengine\api\datastore.py" in __setitem__
  868.  datastore_types.ValidateProperty(name, value)
File "google_appengine\google\appengine\api\datastore_types.py" in ValidateProperty
  1529. 'Unsupported type for property %s: %s' % (name, v.__class__))

generator object is generated by djangotoolbox but GAE is not accepting the generator type. Code from djangotoolbox/db/base.py

生成器对象由djangotoolbox生成,但GAE不接受生成器类型。来自djangotoolbox / db / base.py的代码

   def _value_for_db_collection(self, value, field, field_kind, db_type,
                                 lookup):
      # Do convert filter parameters.
        if lookup:
            # Special case where we are looking for an empty list
            if lookup == 'exact' and db_type == 'list' and value == u'[]':
                return []
            value = self._value_for_db(value, subfield,
                                       subkind, db_subtype, lookup)

        # Convert list/set items or dict values.
        else:
            if field_kind == 'DictField':

                # Generator yielding pairs with converted values.
                value = (
                    (key, self._value_for_db(subvalue, subfield,
                                             subkind, db_subtype, lookup))
                    for key, subvalue in value.iteritems())

                # Return just a dict, a once-flattened list;
                if db_type == 'dict':
                    return dict(value)
                elif db_type == 'list':
                    return list(item for pair in value for item in pair)

            else:

                # Generator producing converted items.
                value = (
                    self._value_for_db(subvalue, subfield,
                                       subkind, db_subtype, lookup)
                    for subvalue in value)

                # "list" may be used for SetField.
                if db_type in 'list':
                    return list(value)
                elif db_type == 'set':
                    # assert field_kind != 'ListField'
                    return set(value)

            # Pickled formats may be used for all collection fields,
            # the fields "natural" type is serialized (something
            # concrete is needed, pickle can't handle generators :-)
            if db_type == 'bytes':
                return pickle.dumps(field._type(value), protocol=2)
            elif db_type == 'string':
                return pickle.dumps(field._type(value))

        # If nothing matched, pass the generator to the back-end.
        return value

AppEngine code that tries to validate the above generator and throws error:

试图验证上述生成器并抛出错误的AppEngine代码:

def ValidateProperty(name, values, read_Only=False):
  ValidateString(name, 'property name', datastore_errors.BadPropertyError)
  values_type = type(values)

  if values_type is tuple:
    raise datastore_errors.BadValueError('May not use tuple property value; property %s is %s.' %(name, repr(values)))

  **if values_type is not list:
    values = [values]**

  if not values:
    raise datastore_errors.BadValueError(
        'May not use the empty list as a property value; property %s is %s.' %
        (name, repr(values)))
  try:
    for v in values:
      prop_validator = _VALIDATE_PROPERTY_VALUES.get(v.__class__)
      if prop_validator is None:
        raise datastore_errors.BadValueError(
          'Unsupported type for property %s: %s' % (name, v.__class__))
      prop_validator(name, v)

  except (KeyError, ValueError, TypeError, IndexError, AttributeError), msg:
    raise datastore_errors.BadValueError(
      'Error type checking values for property %s: %s' % (name, msg))

1 个解决方案

#1


0  

Made change in following code (** new code marked **) and issue seems fixed (maybe I have done something wrong which requires this fix:

对以下代码进行了更改(**新代码标记为**)并且问题似乎已修复(可能我做错了需要此修复:

def _value_for_db_collection(self, value, field, field_kind, db_type,
                                 lookup):
    subfield, subkind, db_subtype = self._convert_as(field.item_field,
                                                     lookup)

    # Do convert filter parameters.
    if lookup:
        # Special case where we are looking for an empty list
        if lookup == 'exact' and db_type == 'list' and value == u'[]':
            return []
        value = self._value_for_db(value, subfield,
                                   subkind, db_subtype, lookup)

    # Convert list/set items or dict values.
    else:
        if field_kind == 'DictField':

            # Generator yielding pairs with converted values.
            value = (
                (key, self._value_for_db(subvalue, subfield,
                                         subkind, db_subtype, lookup))
                for key, subvalue in value.iteritems())

            # Return just a dict, a once-flattened list;
            if db_type == 'dict':
                return dict(value)
            elif db_type == 'list':
                return list(item for pair in value for item in pair)

        else:

            # Generator producing converted items.
            value = (
                self._value_for_db(subvalue, subfield,
                                   subkind, db_subtype, lookup)
                for subvalue in value)

            # "list" may be used for SetField.
            if db_type in 'list':
                return list(value)
            elif db_type == 'set':
                # assert field_kind != 'ListField'
                return set(value)

        # Pickled formats may be used for all collection fields,
        # the fields "natural" type is serialized (something
        # concrete is needed, pickle can't handle generators :-)
        if db_type == 'bytes':
            return pickle.dumps(field._type(value), protocol=2)
        elif db_type == 'string':
            return pickle.dumps(field._type(value))

    # If nothing matched, pass the generator to the back-end.

    #****************NEW CODE - begin **********************
    import types
    if type(value) is types.GeneratorType:
        value = list(value)
    #****************NEW CODE - end****************************

return value

推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了如何将CIM_DateTime解析为.Net DateTime,并分享了解析过程中可能遇到的问题和解决方法。通过使用DateTime.ParseExact方法和适当的格式字符串,可以成功解析CIM_DateTime字符串。同时还提供了关于WMI和字符串格式的相关信息。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文讨论了Kotlin中扩展函数的一些惯用用法以及其合理性。作者认为在某些情况下,定义扩展函数没有意义,但官方的编码约定支持这种方式。文章还介绍了在类之外定义扩展函数的具体用法,并讨论了避免使用扩展函数的边缘情况。作者提出了对于扩展函数的合理性的质疑,并给出了自己的反驳。最后,文章强调了在编写Kotlin代码时可以自由地使用扩展函数的重要性。 ... [详细]
  • Windows7 64位系统安装PLSQL Developer的步骤和注意事项
    本文介绍了在Windows7 64位系统上安装PLSQL Developer的步骤和注意事项。首先下载并安装PLSQL Developer,注意不要安装在默认目录下。然后下载Windows 32位的oracle instant client,并解压到指定路径。最后,按照自己的喜好对解压后的文件进行命名和压缩。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
author-avatar
ys2011一号_139
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有