better db_field validation (#1547)

This commit is contained in:
Stefan Wójcik 2017-05-07 21:11:14 -04:00 committed by GitHub
parent c00914bea2
commit 03ff61d113
2 changed files with 9 additions and 2 deletions

View File

@ -81,7 +81,14 @@ class BaseField(object):
self.sparse = sparse
self._owner_document = None
# Validate the db_field
# Make sure db_field is a string (if it's explicitly defined).
if (
self.db_field is not None and
not isinstance(self.db_field, six.string_types)
):
raise TypeError('db_field should be a string.')
# Make sure db_field doesn't contain any forbidden characters.
if isinstance(self.db_field, six.string_types) and (
'.' in self.db_field or
'\0' in self.db_field or

View File

@ -242,7 +242,7 @@ class InstanceTest(unittest.TestCase):
Zoo.drop_collection()
class Zoo(Document):
animals = ListField(GenericReferenceField(Animal))
animals = ListField(GenericReferenceField())
# Save a reference to each animal
zoo = Zoo(animals=Animal.objects)