Compare commits
8 Commits
db-field-v
...
add-rename
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36193e2f8b | ||
|
|
b7ec587e5b | ||
|
|
47c58bce2b | ||
|
|
96e95ac533 | ||
|
|
b013a065f7 | ||
|
|
c6cc013617 | ||
|
|
f4e1d80a87 | ||
|
|
91dad4060f |
@@ -14,7 +14,7 @@ Before starting to write code, look for existing `tickets
|
|||||||
<https://github.com/MongoEngine/mongoengine/issues?state=open>`_ or `create one
|
<https://github.com/MongoEngine/mongoengine/issues?state=open>`_ or `create one
|
||||||
<https://github.com/MongoEngine/mongoengine/issues>`_ for your specific
|
<https://github.com/MongoEngine/mongoengine/issues>`_ for your specific
|
||||||
issue or feature request. That way you avoid working on something
|
issue or feature request. That way you avoid working on something
|
||||||
that might not be of interest or that has already been addressed. If in doubt
|
that might not be of interest or that has already been addressed. If in doubt
|
||||||
post to the `user group <http://groups.google.com/group/mongoengine-users>`
|
post to the `user group <http://groups.google.com/group/mongoengine-users>`
|
||||||
|
|
||||||
Supported Interpreters
|
Supported Interpreters
|
||||||
|
|||||||
18
README.rst
18
README.rst
@@ -57,7 +57,7 @@ Some simple examples of what MongoEngine code looks like:
|
|||||||
|
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
title = StringField(required=True, max_length=200)
|
title = StringField(required=True, max_length=200)
|
||||||
posted = DateTimeField(default=datetime.datetime.now)
|
posted = DateTimeField(default=datetime.datetime.utcnow)
|
||||||
tags = ListField(StringField(max_length=50))
|
tags = ListField(StringField(max_length=50))
|
||||||
meta = {'allow_inheritance': True}
|
meta = {'allow_inheritance': True}
|
||||||
|
|
||||||
@@ -87,17 +87,18 @@ Some simple examples of what MongoEngine code looks like:
|
|||||||
... print
|
... print
|
||||||
...
|
...
|
||||||
|
|
||||||
>>> len(BlogPost.objects)
|
# Count all blog posts and its subtypes
|
||||||
|
>>> BlogPost.objects.count()
|
||||||
2
|
2
|
||||||
>>> len(TextPost.objects)
|
>>> TextPost.objects.count()
|
||||||
1
|
1
|
||||||
>>> len(LinkPost.objects)
|
>>> LinkPost.objects.count()
|
||||||
1
|
1
|
||||||
|
|
||||||
# Find tagged posts
|
# Count tagged posts
|
||||||
>>> len(BlogPost.objects(tags='mongoengine'))
|
>>> BlogPost.objects(tags='mongoengine').count()
|
||||||
2
|
2
|
||||||
>>> len(BlogPost.objects(tags='mongodb'))
|
>>> BlogPost.objects(tags='mongodb').count()
|
||||||
1
|
1
|
||||||
|
|
||||||
Tests
|
Tests
|
||||||
@@ -130,8 +131,7 @@ Community
|
|||||||
<http://groups.google.com/group/mongoengine-users>`_
|
<http://groups.google.com/group/mongoengine-users>`_
|
||||||
- `MongoEngine Developers mailing list
|
- `MongoEngine Developers mailing list
|
||||||
<http://groups.google.com/group/mongoengine-dev>`_
|
<http://groups.google.com/group/mongoengine-dev>`_
|
||||||
- `#mongoengine IRC channel <http://webchat.freenode.net/?channels=mongoengine>`_
|
|
||||||
|
|
||||||
Contributing
|
Contributing
|
||||||
============
|
============
|
||||||
We welcome contributions! see the `Contribution guidelines <https://github.com/MongoEngine/mongoengine/blob/master/CONTRIBUTING.rst>`_
|
We welcome contributions! See the `Contribution guidelines <https://github.com/MongoEngine/mongoengine/blob/master/CONTRIBUTING.rst>`_
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ the :attr:`host` to
|
|||||||
corresponding parameters in :func:`~mongoengine.connect`: ::
|
corresponding parameters in :func:`~mongoengine.connect`: ::
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
name='test',
|
db='test',
|
||||||
username='user',
|
username='user',
|
||||||
password='12345',
|
password='12345',
|
||||||
host='mongodb://admin:qwerty@localhost/production'
|
host='mongodb://admin:qwerty@localhost/production'
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ __all__ = ('UPDATE_OPERATORS', 'get_document', '_document_registry')
|
|||||||
|
|
||||||
UPDATE_OPERATORS = set(['set', 'unset', 'inc', 'dec', 'pop', 'push',
|
UPDATE_OPERATORS = set(['set', 'unset', 'inc', 'dec', 'pop', 'push',
|
||||||
'push_all', 'pull', 'pull_all', 'add_to_set',
|
'push_all', 'pull', 'pull_all', 'add_to_set',
|
||||||
'set_on_insert', 'min', 'max'])
|
'set_on_insert', 'min', 'max', 'rename'])
|
||||||
|
|
||||||
|
|
||||||
_document_registry = {}
|
_document_registry = {}
|
||||||
|
|||||||
@@ -402,9 +402,11 @@ class BaseDocument(object):
|
|||||||
raise ValidationError(message, errors=errors)
|
raise ValidationError(message, errors=errors)
|
||||||
|
|
||||||
def to_json(self, *args, **kwargs):
|
def to_json(self, *args, **kwargs):
|
||||||
"""Converts a document to JSON.
|
"""Convert this document to JSON.
|
||||||
:param use_db_field: Set to True by default but enables the output of the json structure with the field names
|
|
||||||
and not the mongodb store db_names in case of set to False
|
:param use_db_field: Serialize field names as they appear in
|
||||||
|
MongoDB (as opposed to attribute names on this document).
|
||||||
|
Defaults to True.
|
||||||
"""
|
"""
|
||||||
use_db_field = kwargs.pop('use_db_field', True)
|
use_db_field = kwargs.pop('use_db_field', True)
|
||||||
return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs)
|
return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs)
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class BaseField(object):
|
|||||||
"""
|
"""
|
||||||
:param db_field: The database field to store this field in
|
:param db_field: The database field to store this field in
|
||||||
(defaults to the name of the field)
|
(defaults to the name of the field)
|
||||||
:param name: Deprecated - use db_field
|
:param name: Depreciated - use db_field
|
||||||
:param required: If the field is required. Whether it has to have a
|
:param required: If the field is required. Whether it has to have a
|
||||||
value or not. Defaults to False.
|
value or not. Defaults to False.
|
||||||
:param default: (optional) The default value for this field if no value
|
:param default: (optional) The default value for this field if no value
|
||||||
@@ -81,17 +81,6 @@ class BaseField(object):
|
|||||||
self.sparse = sparse
|
self.sparse = sparse
|
||||||
self._owner_document = None
|
self._owner_document = None
|
||||||
|
|
||||||
# Validate the db_field
|
|
||||||
if isinstance(self.db_field, six.string_types) and (
|
|
||||||
'.' in self.db_field or
|
|
||||||
'\0' in self.db_field or
|
|
||||||
self.db_field.startswith('$')
|
|
||||||
):
|
|
||||||
raise ValueError(
|
|
||||||
'field names cannot contain dots (".") or null characters '
|
|
||||||
'("\\0"), and they must not start with a dollar sign ("$").'
|
|
||||||
)
|
|
||||||
|
|
||||||
# Detect and report conflicts between metadata and base properties.
|
# Detect and report conflicts between metadata and base properties.
|
||||||
conflicts = set(dir(self)) & set(kwargs)
|
conflicts = set(dir(self)) & set(kwargs)
|
||||||
if conflicts:
|
if conflicts:
|
||||||
|
|||||||
@@ -313,6 +313,9 @@ class Document(BaseDocument):
|
|||||||
.. versionchanged:: 0.10.7
|
.. versionchanged:: 0.10.7
|
||||||
Add signal_kwargs argument
|
Add signal_kwargs argument
|
||||||
"""
|
"""
|
||||||
|
if self._meta.get('abstract'):
|
||||||
|
raise InvalidDocumentError('Cannot save an abstract document.')
|
||||||
|
|
||||||
signal_kwargs = signal_kwargs or {}
|
signal_kwargs = signal_kwargs or {}
|
||||||
signals.pre_save.send(self.__class__, document=self, **signal_kwargs)
|
signals.pre_save.send(self.__class__, document=self, **signal_kwargs)
|
||||||
|
|
||||||
@@ -828,7 +831,6 @@ class Document(BaseDocument):
|
|||||||
""" Lists all of the indexes that should be created for given
|
""" Lists all of the indexes that should be created for given
|
||||||
collection. It includes all the indexes from super- and sub-classes.
|
collection. It includes all the indexes from super- and sub-classes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if cls._meta.get('abstract'):
|
if cls._meta.get('abstract'):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
@@ -933,7 +933,20 @@ class BaseQuerySet(object):
|
|||||||
key = '.'.join(parts)
|
key = '.'.join(parts)
|
||||||
cleaned_fields.append((key, value))
|
cleaned_fields.append((key, value))
|
||||||
|
|
||||||
fields = sorted(cleaned_fields, key=operator.itemgetter(1))
|
# Sort fields by their values, explicitly excluded fields first, then
|
||||||
|
# explicitly included, and then more complicated operators such as
|
||||||
|
# $slice.
|
||||||
|
def _sort_key(field_tuple):
|
||||||
|
key, value = field_tuple
|
||||||
|
if isinstance(value, (int)):
|
||||||
|
return value # 0 for exclusion, 1 for inclusion
|
||||||
|
else:
|
||||||
|
return 2 # so that complex values appear last
|
||||||
|
|
||||||
|
fields = sorted(cleaned_fields, key=_sort_key)
|
||||||
|
|
||||||
|
# Clone the queryset, group all fields by their value, convert
|
||||||
|
# each of them to db_fields, and set the queryset's _loaded_fields
|
||||||
queryset = self.clone()
|
queryset = self.clone()
|
||||||
for value, group in itertools.groupby(fields, lambda x: x[1]):
|
for value, group in itertools.groupby(fields, lambda x: x[1]):
|
||||||
fields = [field for field, value in group]
|
fields = [field for field, value in group]
|
||||||
|
|||||||
@@ -233,8 +233,7 @@ def update(_doc_cls=None, **update):
|
|||||||
# Support decrement by flipping a positive value's sign
|
# Support decrement by flipping a positive value's sign
|
||||||
# and using 'inc'
|
# and using 'inc'
|
||||||
op = 'inc'
|
op = 'inc'
|
||||||
if value > 0:
|
value = -value
|
||||||
value = -value
|
|
||||||
elif op == 'add_to_set':
|
elif op == 'add_to_set':
|
||||||
op = 'addToSet'
|
op = 'addToSet'
|
||||||
elif op == 'set_on_insert':
|
elif op == 'set_on_insert':
|
||||||
|
|||||||
@@ -435,6 +435,15 @@ class InstanceTest(unittest.TestCase):
|
|||||||
|
|
||||||
person.to_dbref()
|
person.to_dbref()
|
||||||
|
|
||||||
|
def test_save_abstract_document(self):
|
||||||
|
"""Saving an abstract document should fail."""
|
||||||
|
class Doc(Document):
|
||||||
|
name = StringField()
|
||||||
|
meta = {'abstract': True}
|
||||||
|
|
||||||
|
with self.assertRaises(InvalidDocumentError):
|
||||||
|
Doc(name='aaa').save()
|
||||||
|
|
||||||
def test_reload(self):
|
def test_reload(self):
|
||||||
"""Ensure that attributes may be reloaded.
|
"""Ensure that attributes may be reloaded.
|
||||||
"""
|
"""
|
||||||
@@ -1223,6 +1232,19 @@ class InstanceTest(unittest.TestCase):
|
|||||||
self.assertEqual(person.name, None)
|
self.assertEqual(person.name, None)
|
||||||
self.assertEqual(person.age, None)
|
self.assertEqual(person.age, None)
|
||||||
|
|
||||||
|
def test_update_rename_operator(self):
|
||||||
|
"""Test the $rename operator."""
|
||||||
|
coll = self.Person._get_collection()
|
||||||
|
doc = self.Person(name='John').save()
|
||||||
|
raw_doc = coll.find_one({'_id': doc.pk})
|
||||||
|
self.assertEqual(set(raw_doc.keys()), set(['_id', '_cls', 'name']))
|
||||||
|
|
||||||
|
doc.update(rename__name='first_name')
|
||||||
|
raw_doc = coll.find_one({'_id': doc.pk})
|
||||||
|
self.assertEqual(set(raw_doc.keys()),
|
||||||
|
set(['_id', '_cls', 'first_name']))
|
||||||
|
self.assertEqual(raw_doc['first_name'], 'John')
|
||||||
|
|
||||||
def test_inserts_if_you_set_the_pk(self):
|
def test_inserts_if_you_set_the_pk(self):
|
||||||
p1 = self.Person(name='p1', id=bson.ObjectId()).save()
|
p1 = self.Person(name='p1', id=bson.ObjectId()).save()
|
||||||
p2 = self.Person(name='p2')
|
p2 = self.Person(name='p2')
|
||||||
|
|||||||
@@ -306,24 +306,6 @@ class FieldTest(unittest.TestCase):
|
|||||||
person.id = '497ce96f395f2f052a494fd4'
|
person.id = '497ce96f395f2f052a494fd4'
|
||||||
person.validate()
|
person.validate()
|
||||||
|
|
||||||
def test_db_field_validation(self):
|
|
||||||
"""Ensure that db_field doesn't accept invalid values."""
|
|
||||||
|
|
||||||
# dot in the name
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
class User(Document):
|
|
||||||
name = StringField(db_field='user.name')
|
|
||||||
|
|
||||||
# name starting with $
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
class User(Document):
|
|
||||||
name = StringField(db_field='$name')
|
|
||||||
|
|
||||||
# name containing a null character
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
class User(Document):
|
|
||||||
name = StringField(db_field='name\0')
|
|
||||||
|
|
||||||
def test_string_validation(self):
|
def test_string_validation(self):
|
||||||
"""Ensure that invalid values cannot be assigned to string fields.
|
"""Ensure that invalid values cannot be assigned to string fields.
|
||||||
"""
|
"""
|
||||||
@@ -3991,25 +3973,30 @@ class FieldTest(unittest.TestCase):
|
|||||||
"""Tests if a `FieldDoesNotExist` exception is raised when trying to
|
"""Tests if a `FieldDoesNotExist` exception is raised when trying to
|
||||||
instanciate a document with a field that's not defined.
|
instanciate a document with a field that's not defined.
|
||||||
"""
|
"""
|
||||||
class Doc(Document):
|
|
||||||
foo = StringField()
|
|
||||||
|
|
||||||
with self.assertRaises(FieldDoesNotExist):
|
class Doc(Document):
|
||||||
|
foo = StringField(db_field='f')
|
||||||
|
|
||||||
|
def test():
|
||||||
Doc(bar='test')
|
Doc(bar='test')
|
||||||
|
|
||||||
|
self.assertRaises(FieldDoesNotExist, test)
|
||||||
|
|
||||||
def test_undefined_field_exception_with_strict(self):
|
def test_undefined_field_exception_with_strict(self):
|
||||||
"""Tests if a `FieldDoesNotExist` exception is raised when trying to
|
"""Tests if a `FieldDoesNotExist` exception is raised when trying to
|
||||||
instanciate a document with a field that's not defined,
|
instanciate a document with a field that's not defined,
|
||||||
even when strict is set to False.
|
even when strict is set to False.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Doc(Document):
|
class Doc(Document):
|
||||||
foo = StringField()
|
foo = StringField(db_field='f')
|
||||||
meta = {'strict': False}
|
meta = {'strict': False}
|
||||||
|
|
||||||
with self.assertRaises(FieldDoesNotExist):
|
def test():
|
||||||
Doc(bar='test')
|
Doc(bar='test')
|
||||||
|
|
||||||
|
self.assertRaises(FieldDoesNotExist, test)
|
||||||
|
|
||||||
def test_long_field_is_considered_as_int64(self):
|
def test_long_field_is_considered_as_int64(self):
|
||||||
"""
|
"""
|
||||||
Tests that long fields are stored as long in mongo, even if long value
|
Tests that long fields are stored as long in mongo, even if long value
|
||||||
|
|||||||
@@ -141,6 +141,16 @@ class OnlyExcludeAllTest(unittest.TestCase):
|
|||||||
self.assertEqual(qs._loaded_fields.as_dict(),
|
self.assertEqual(qs._loaded_fields.as_dict(),
|
||||||
{'b': {'$slice': 5}})
|
{'b': {'$slice': 5}})
|
||||||
|
|
||||||
|
def test_mix_slice_with_other_fields(self):
|
||||||
|
class MyDoc(Document):
|
||||||
|
a = ListField()
|
||||||
|
b = ListField()
|
||||||
|
c = ListField()
|
||||||
|
|
||||||
|
qs = MyDoc.objects.fields(a=1, b=0, slice__c=2)
|
||||||
|
self.assertEqual(qs._loaded_fields.as_dict(),
|
||||||
|
{'c': {'$slice': 2}, 'a': 1})
|
||||||
|
|
||||||
def test_only(self):
|
def test_only(self):
|
||||||
"""Ensure that QuerySet.only only returns the requested fields.
|
"""Ensure that QuerySet.only only returns the requested fields.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1826,6 +1826,11 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
post.reload()
|
post.reload()
|
||||||
self.assertEqual(post.hits, 10)
|
self.assertEqual(post.hits, 10)
|
||||||
|
|
||||||
|
# Negative dec operator is equal to a positive inc operator
|
||||||
|
BlogPost.objects.update_one(dec__hits=-1)
|
||||||
|
post.reload()
|
||||||
|
self.assertEqual(post.hits, 11)
|
||||||
|
|
||||||
BlogPost.objects.update(push__tags='mongo')
|
BlogPost.objects.update(push__tags='mongo')
|
||||||
post.reload()
|
post.reload()
|
||||||
self.assertTrue('mongo' in post.tags)
|
self.assertTrue('mongo' in post.tags)
|
||||||
|
|||||||
Reference in New Issue
Block a user