Implemented Choices for GenericReferenceFields

Refs mongoengine/mongoengine#13
This commit is contained in:
Ross Lawley
2012-05-09 13:21:53 +01:00
parent aeebdfec51
commit 2aa8b04c21
4 changed files with 82 additions and 4 deletions

View File

@@ -223,11 +223,10 @@ class BaseField(object):
pass
def _validate(self, value):
from mongoengine import EmbeddedDocument
from mongoengine import Document, EmbeddedDocument
# check choices
if self.choices:
is_cls = isinstance(value, EmbeddedDocument)
is_cls = isinstance(value, (Document, EmbeddedDocument))
value_to_check = value.__class__ if is_cls else value
err_msg = 'an instance' if is_cls else 'one'
if isinstance(self.choices[0], (list, tuple)):

View File

@@ -441,6 +441,9 @@ class GenericEmbeddedDocumentField(BaseField):
:class:`~mongoengine.EmbeddedDocument` to be stored.
Only valid values are subclasses of :class:`~mongoengine.EmbeddedDocument`.
..note :: You can use the choices param to limit the acceptable
EmbeddedDocument types
"""
def prepare_query_value(self, op, value):
@@ -701,6 +704,8 @@ class GenericReferenceField(BaseField):
..note :: Any documents used as a generic reference must be registered in the
document registry. Importing the model will automatically register it.
..note :: You can use the choices param to limit the acceptable Document types
.. versionadded:: 0.3
"""