From adb7bbeea0b95975eca21cbe090eca08b4dd9315 Mon Sep 17 00:00:00 2001 From: Karim Allah Date: Sun, 18 Sep 2011 19:48:33 +0200 Subject: [PATCH 1/2] Being compatible with non-django style chioces --- mongoengine/base.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index c4bcee1e..92a5b71a 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -139,9 +139,14 @@ class BaseField(object): def _validate(self, value): # check choices if self.choices is not None: - option_keys = [option_key for option_key, option_value in self.choices] - if value not in option_keys: - raise ValidationError("Value must be one of %s." % unicode(option_keys)) + if type(choices[0]) is tuple: + option_keys = [option_key for option_key, option_value in self.choices] + if value not in option_keys: + raise ValidationError("Value must be one of %s." % unicode(option_keys)) + else: + if value not in self.choices: + raise ValidationError("Value must be one of %s." % unicode(self.choices)) + # check validation argument if self.validation is not None: From c081aca79431e7e060e66c2e4d4da88cc66ed8ae Mon Sep 17 00:00:00 2001 From: Karim Allah Date: Sun, 25 Sep 2011 18:58:40 +0200 Subject: [PATCH 2/2] Fixing dereferencing when the dereferenced-document wasn't found. --- mongoengine/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mongoengine/base.py b/mongoengine/base.py index 92a5b71a..d02848f1 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -10,6 +10,7 @@ import pymongo import pymongo.objectid import operator from functools import partial +from bson.dbref import DBRef class NotRegistered(Exception): @@ -97,6 +98,9 @@ class BaseField(object): # Get value from document instance if available, if not use default value = instance._data.get(self.name) + if isinstance(value, DBRef): + raise ValueError("Can't dereference from the given DBRef (%s)" % str(value)) + if value is None: value = self.default # Allow callable default values