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

5 投票
3 回答
520 浏览
提问于 2025-04-16 18:19

以下代码

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(dispatcher, 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, hook) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py",第 2834 行,在 ExecuteOrImportScript 中 exec module_code in script_module.dict 文件 "D:\svn\language\Web\src\controller.py",第 5 行,在 from model import * 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py",第 1505 行,在 Decorate 中 return func(self, *args, **kwargs) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py",第 2450 行,在 load_module 中 return self.FindAndLoadModule(submodule, fullname, search_path) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py",第 1505 行,在 Decorate 中 return func(self, *args, **kwargs) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py",第 2339 行,在 FindAndLoadModule 中 description) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py",第 1505 行,在 Decorate 中 return func(self, *args, **kwargs) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py",第 2282 行,在 LoadModuleRestricted 中 description) 文件 "D:\svn\language\Web\src\model.py",第 24 行,在 class Translation(db.Model): 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py",第 500 行,在 init _initialize_properties(cls, name, bases, dct) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py",第 415 行,在 _initialize_properties 中 attr.property_config(model_class, attr_name) 文件 "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py",第 3461 行,在 property_config self.collection_name)) DuplicatePropertyError: 类 Expression 已经有属性 translation_set

如何绕过这个限制?

3 个回答

5

给它们起不同的集合名称:

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')
11

给集合起不同的名字

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')

每个 db.ReferenceProperty 默认会在被引用的模型中创建一个名为 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)

每个人(Human)默认会有一个叫 ownedcar_set 的属性。如果你两次引用同一个模型,就会出现集合名字冲突的问题。所以你应该给集合加上名字,以便区分这些集合。

更多参考内容可以在 这里 找到。

6

在编程中,有时候我们需要把一些代码放在一个地方,然后在其他地方调用它。这样做的好处是可以避免重复写同样的代码,让程序更整洁。

比如说,你可能会写一个函数,这个函数可以完成某个特定的任务。然后在需要的时候,只要调用这个函数就可以了,而不需要每次都写一遍代码。

这就像是你在厨房里做饭,如果你已经学会了怎么做一个菜,你就可以在不同的场合多次做这个菜,而不需要每次都从头开始学习。

总之,编程中的这种做法可以让我们的代码更简洁、更易于管理,也让我们在需要修改的时候,只需改一处就可以了。

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

撰写回答