Skip to content

Instantly share code, notes, and snippets.

@austing
Created March 12, 2011 03:24
Show Gist options
  • Save austing/867005 to your computer and use it in GitHub Desktop.
Save austing/867005 to your computer and use it in GitHub Desktop.
ModelChoiceField for mongoengine
class MongoModelChoiceField(ModelChoiceField):
# this works if you pass the field a queryset keyword argument. I doubt it works with a model as the argument
# does not support to_field_name, but that would be easy
def to_python(self, value):
if value in EMPTY_VALUES:
return None
# mongoengine has no deepcopy as yet, so we can't do:
# querycopy = self.queryset.copy()
# value = querycopy.filter(pk=value.pk).first()
# instead:
for x in self.queryset:
if str(x.id) == str(value):
return x
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment