From 208a467b24a5a0437f0a9a09e9b9d9caceef3cd9 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Mon, 28 Nov 2011 07:05:54 -0800 Subject: [PATCH] Added dictfield check for Int keys Fixes #371 --- mongoengine/fields.py | 4 +++- tests/fields.py | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 23d9e453..a0dcdb20 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -552,7 +552,9 @@ class DictField(ComplexBaseField): if not isinstance(value, dict): self.error('Only dictionaries may be used in a DictField') - if any(('.' in k or '$' in k) for k in value): + if any(k for k in value.keys() if not isinstance(k, basestring)): + self.error('Invalid dictionary key - documents must have only string keys') + if any(('.' in k or '$' in k) for k in value.keys()): self.error('Invalid dictionary key name - keys may not contain "."' ' or "$" characters') super(DictField, self).validate(value) diff --git a/tests/fields.py b/tests/fields.py index 5f19e336..56d216a6 100644 --- a/tests/fields.py +++ b/tests/fields.py @@ -636,6 +636,9 @@ class FieldTest(unittest.TestCase): post.info = {'the.title': 'test'} self.assertRaises(ValidationError, post.validate) + post.info = {1: 'test'} + self.assertRaises(ValidationError, post.validate) + post.info = {'title': 'test'} post.save()