English 中文(简体)
如何从另一个模型引用同一个模型两次?
原标题:How to reference the same Model twice from another one?

以下代码

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

会产生以下错误:

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

如何规避这一限制?

最佳回答
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= 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来区分集合。

更多参考此处

为它们提供不同的集合名称:

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 )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签