Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference the same Model twice from another one?

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?

like image 352
Jader Dias Avatar asked May 25 '11 14:05

Jader Dias


2 Answers

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

like image 170
Abdul Kader Avatar answered Sep 28 '22 09:09

Abdul Kader


class Translation(db.Model):
    origin = db.ReferenceProperty(Expression, required=True, collection_name='translation_origins')
    target = db.ReferenceProperty(Expression, required=True, collection_name='translation_targets')
like image 24
Tudor Constantin Avatar answered Sep 28 '22 11:09

Tudor Constantin