Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将临时信息附加到appengine ndb模型实例?_Python_Google App Engine_App Engine Ndb - Fatal编程技术网

Python 如何将临时信息附加到appengine ndb模型实例?

Python 如何将临时信息附加到appengine ndb模型实例?,python,google-app-engine,app-engine-ndb,Python,Google App Engine,App Engine Ndb,我正在尝试为我的模型设置一个属性 class Foo(ndb.Model): name = ndb.StringProperty() 从memcache中检索Foo对象 如果不在memcache中,则从数据库中查询这里我想向Foo实例附加附加信息Foo.special\u name=“不要持久化此” 将此对象从memcache传递给我的自定义JsonEncoder 将此对象转储到字典,foo={name:“hello”,special_name:“don't persist this”}

我正在尝试为我的模型设置一个属性

class Foo(ndb.Model):
  name = ndb.StringProperty()
  • 从memcache中检索Foo对象
  • 如果不在memcache中,则从数据库中查询这里我想向Foo实例附加附加信息
    Foo.special\u name=“不要持久化此”
  • 将此对象从memcache传递给我的自定义JsonEncoder
  • 将此对象转储到字典,foo={name:“hello”,special_name:“don't persist this”}
  • 抱歉,我不能设置非字段的ndb.Model类的属性

    我害怕使用ndb.Expando,因为我担心这个对象会被持久化,并且
    特殊名称
    会保存到我的数据库中

    foo
    添加临时一次性价值的最干净方法是什么

    编辑:

    >类别Foo(ndb.Model):
    name=ndb.StringProperty()
    >>>f=Foo()
    >>>f.put()
    钥匙('Foo',26740114379063)
    >>>f.bar=123
    >>>f
    Foo(name=None)
    >>>酒吧
    回溯(最近一次呼叫最后一次):
    get中第267行的文件“/base/data/home/apps/shell/1.335852500710379686/shell.py”
    exec在语句\u模块中编译__
    文件“”,第1行,在
    AttributeError:“Foo”对象没有属性“bar”
    >>>设置属性(f'条形',123)
    >>>f
    Foo(name=None)
    >>>酒吧
    回溯(最近一次呼叫最后一次):
    get中第267行的文件“/base/data/home/apps/shell/1.335852500710379686/shell.py”
    exec在语句\u模块中编译__
    文件“”,第1行,在
    AttributeError:“Foo”对象没有属性“bar”
    >>>设置属性(f'条形',123)
    >>>getattr(f,‘bar’)
    回溯(最近一次呼叫最后一次):
    get中第267行的文件“/base/data/home/apps/shell/1.335852500710379686/shell.py”
    exec在语句\u模块中编译__
    文件“”,第1行,在
    AttributeError:“Foo”对象没有属性“bar”
    
    “我无法设置非字段的ndb.Model类的属性。”

    你为什么这么想?模型实例只是对象,与Python[*]中的任何其他对象一样,您可以在它们上设置任意属性


    [*]:(只要类没有定义
    \uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?我试过这样做:f=Foo();f、 put();f、 bar=123;>>>f、 bar AttributeError:'Foo'对象没有属性'bar'`好吧,看起来我使用的是appengine API中过时的shell。这在我当前的主机上工作,谢谢
    
    >>> class Foo(ndb.Model):
      name = ndb.StringProperty()
    >>> f = Foo()
    >>> f.put()
    Key('Foo', 26740114379063)
    >>> f.bar = 123 
    >>> f
    Foo(name=None)
    >>> f.bar
    Traceback (most recent call last):
      File "/base/data/home/apps/shell/1.335852500710379686/shell.py", line 267, in get
        exec compiled in statement_module.__dict__
      File "<string>", line 1, in <module>
    AttributeError: 'Foo' object has no attribute 'bar'
    >>> setattr(f, 'bar', 123)
    >>> f
    Foo(name=None)
    >>> f.bar
    Traceback (most recent call last):
      File "/base/data/home/apps/shell/1.335852500710379686/shell.py", line 267, in get
        exec compiled in statement_module.__dict__
      File "<string>", line 1, in <module>
    AttributeError: 'Foo' object has no attribute 'bar'
    >>> setattr(f, 'bar', 123)
    >>> getattr(f, 'bar')
    Traceback (most recent call last):
      File "/base/data/home/apps/shell/1.335852500710379686/shell.py", line 267, in get
        exec compiled in statement_module.__dict__
      File "<string>", line 1, in <module>
    AttributeError: 'Foo' object has no attribute 'bar'