get dddp to get correct collection and not throw an error

This commit is contained in:
craig 2015-12-20 00:41:05 +11:00
parent 8cbd3f3389
commit d517e28e8e

View file

@ -230,6 +230,8 @@ class CollectionMeta(APIMeta):
"""DDP Collection metaclass."""
model_name_map = {}
def __new__(mcs, name, bases, attrs):
"""Create a new Collection class."""
attrs.update(
@ -240,6 +242,10 @@ class CollectionMeta(APIMeta):
attrs.update(
name=model_name(model),
)
if model and attrs.get('name'):
if model._meta.label_lower:
model = model._meta.label_lower
CollectionMeta.model_name_map.update({model: attrs.get('name')})
return super(CollectionMeta, mcs).__new__(mcs, name, bases, attrs)
@ -605,7 +611,11 @@ class DDP(APIMixin):
def get_col_by_name(self, name):
"""Return collection instance for given name."""
return self._registry[COLLECTION_PATH_FORMAT.format(name=name)]
try:
return self._registry[COLLECTION_PATH_FORMAT.format(name=name)]
except:
name = Collection.model_name_map[str(name)]
return self._registry[COLLECTION_PATH_FORMAT.format(name=name)]
def get_pub_by_name(self, name):
"""Return publication instance for given name."""