如何从另一个模型引用同一模型两次?

发布于 2024-11-09 09:00:54 字数 2878 浏览 2 评论 0原文

以下代码

class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True)
    target = db.ReferenceProperty(Expression, required=True)

产生以下错误:

回溯(最近一次调用最后一次): 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 4053 行,位于 _HandleRequest 中 self._Dispatch(调度程序,self.rfile,outfile,env_dict) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 3977 行,位于 _Dispatch base_env_dict = env_dict) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 588 行,位于 Dispatch 中 base_env_dict=base_env_dict) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 3050 行,位于 Dispatch 中 self._module_dict) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 2954 行,位于 ExecuteCGI 中 reset_modules = exec_script(handler_path, cgi_path, 钩子) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 2834 行,位于 ExecuteOrImportScript 中 在 script_module.dict 中执行 module_code 文件“D:\svn\language\Web\src\controller.py”,第 5 行,位于 从模型导入 * 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 1505 行,位于装饰中 返回 func(self, *args, **kwargs) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 2450 行,位于 load_module 中 返回 self.FindAndLoadModule(子模块, 全名, search_path) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 1505 行,位于装饰中 返回 func(self, *args, **kwargs) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 2339 行,位于 FindAndLoadModule 中 描述) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 1505 行,位于装饰中 返回 func(self, *args, **kwargs) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py”,第 2282 行,位于 LoadModuleRestricted 中 描述) 文件“D:\svn\language\Web\src\model.py”,第 24 行,位于 类翻译(db.Model): 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py”,第 500 行,位于 init 中 _initialize_properties(cls,名称,基础,dct) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py”,第 415 行,位于 _initialize_properties 中 attr.property_config(模型类,attr_name) 文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py”,第 3461 行,位于 property_config 中 self.collection_name)) DuplicatePropertyError:类表达式已经具有属性translation_set

如何规避此限制?

The following code

class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True)
    target = db.ReferenceProperty(Expression, required=True)

produces the following error:

Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 4053, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3977, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 588, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3050, in Dispatch
self._module_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2954, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2834, in ExecuteOrImportScript
exec module_code in script_module.dict
File "D:\svn\language\Web\src\controller.py", line 5, in
from model import *
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2450, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2339, in FindAndLoadModule
description)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2282, in LoadModuleRestricted
description)
File "D:\svn\language\Web\src\model.py", line 24, in
class Translation(db.Model):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py", line 500, in init
_initialize_properties(cls, name, bases, dct)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py", line 415, in _initialize_properties
attr.property_config(model_class, attr_name)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py", line 3461, in property_config
self.collection_name))
DuplicatePropertyError: Class Expression already has property translation_set

How to circumvent this limitation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜无邪 2024-11-16 09:00:54

为每个 db.ReferenceProperty指定不同的集合名称,

class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True,collection_name='origin_translation_set')
    target = db.ReferenceProperty(Expression, required=True,collection_name='target_translation_set')

默认情况下会在引用的模型中创建一个名为 referencedmodelname_set 的集合。

例如:

class OwnedCar(db.Model):
   brand  =  db.StringProperty(required=True)
   owner  =  db.ReferenceProperty(Human, required=True)

class Human(db.Model):
    name    = db.StringProperty(required=True)
    drives  = db.ReferenceProperty(reference_class=Car)

默认情况下,每个人都会有一个名为ownedcar_set 的属性。如果您两次引用同一模型,则会产生 collection_name 冲突。所以你应该添加collection_name来区分集合。

更多参考此处

Give distinct collection names

class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True,collection_name='origin_translation_set')
    target = db.ReferenceProperty(Expression, required=True,collection_name='target_translation_set')

every db.ReferenceProperty by default creates a collection named referencedmodelname_set in the referred model.

For eg:

class OwnedCar(db.Model):
   brand  =  db.StringProperty(required=True)
   owner  =  db.ReferenceProperty(Human, required=True)

class Human(db.Model):
    name    = db.StringProperty(required=True)
    drives  = db.ReferenceProperty(reference_class=Car)

Every Human by default will have an attribute called ownedcar_set by default. If you are referring to the same model twice, this creates the collection_name conflict. So you should add collection_name to differentiate the collections.

More reference here

多情癖 2024-11-16 09:00:54
class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True, collection_name='translation_origins')
    target = db.ReferenceProperty(Expression, required=True, collection_name='translation_targets')
class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True, collection_name='translation_origins')
    target = db.ReferenceProperty(Expression, required=True, collection_name='translation_targets')
灰色世界里的红玫瑰 2024-11-16 09:00:54

给它们指定不同的集合名称:

class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True,
                                  collection_name='origin_translation_set')
    target = db.ReferenceProperty(Expression, required=True,
                                  collection_name='target_translation_set')

Give them distinct collection names:

class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True,
                                  collection_name='origin_translation_set')
    target = db.ReferenceProperty(Expression, required=True,
                                  collection_name='target_translation_set')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文