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

时间:2011-05-25 14:28:49

标签: python google-app-engine foreign-keys google-cloud-datastore

以下代码

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

产生以下错误:

  

追踪(最近一次通话):     _HandleRequest中的文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第4053行       self._Dispatch(dispatcher,self.rfile,outfile,env_dict)     _Dispatch中的文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第3977行       base_env_dict = env_dict)     在Dispatch中输入文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第588行       base_env_dict = base_env_dict)     在Dispatch中输入文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第3050行       self._module_dict)     在ExecuteCGI中输入文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第2954行       reset_modules = exec_script(handler_path,cgi_path,hook)     在ExecuteOrImportScript中的文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第2834行       script_module中的exec module_code。 dict     文件“D:\ svn \ language \ Web \ src \ controller.py”,第5行,in       来自模型导入*     文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第1505行,装饰       return func(self,* args,** kwargs)     在load_module中输入文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第2450行       return self.FindAndLoadModule(submodule,fullname,search_path)     文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第1505行,装饰       return func(self,* args,** kwargs)     在FindAndLoadModule中输入文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第2339行       描述)     文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第1505行,装饰       return func(self,* args,** kwargs)     在LoadModuleRestricted中的文件“C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ tools \ dev_appserver.py”,第2282行       描述)     文件“D:\ svn \ language \ Web \ src \ model.py”,第24行,in       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:类表达式已具有属性translation_set

如何规避这种限制?

3 个答案:

答案 0 :(得分: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)

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

更多参考here

答案 1 :(得分: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')

答案 2 :(得分: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')