'set__comments__0__body="asdf"' syntax works
This commit is contained in:
parent
95c2643f63
commit
36034ee15f
@ -22,7 +22,7 @@ class BaseField(object):
|
|||||||
may be added to subclasses of `Document` to define a document's schema.
|
may be added to subclasses of `Document` to define a document's schema.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Fields may have _types inserted into indexes by default
|
# Fields may have _types inserted into indexes by default
|
||||||
_index_with_types = True
|
_index_with_types = True
|
||||||
_geo_index = False
|
_geo_index = False
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ class BaseField(object):
|
|||||||
creation_counter = 0
|
creation_counter = 0
|
||||||
auto_creation_counter = -1
|
auto_creation_counter = -1
|
||||||
|
|
||||||
def __init__(self, db_field=None, name=None, required=False, default=None,
|
def __init__(self, db_field=None, name=None, required=False, default=None,
|
||||||
unique=False, unique_with=None, primary_key=False,
|
unique=False, unique_with=None, primary_key=False,
|
||||||
validation=None, choices=None):
|
validation=None, choices=None):
|
||||||
self.db_field = (db_field or name) if not primary_key else '_id'
|
self.db_field = (db_field or name) if not primary_key else '_id'
|
||||||
@ -57,7 +57,7 @@ class BaseField(object):
|
|||||||
BaseField.creation_counter += 1
|
BaseField.creation_counter += 1
|
||||||
|
|
||||||
def __get__(self, instance, owner):
|
def __get__(self, instance, owner):
|
||||||
"""Descriptor for retrieving a value from a field in a document. Do
|
"""Descriptor for retrieving a value from a field in a document. Do
|
||||||
any necessary conversion between Python and MongoDB types.
|
any necessary conversion between Python and MongoDB types.
|
||||||
"""
|
"""
|
||||||
if instance is None:
|
if instance is None:
|
||||||
@ -167,8 +167,8 @@ class DocumentMetaclass(type):
|
|||||||
superclasses.update(base._superclasses)
|
superclasses.update(base._superclasses)
|
||||||
|
|
||||||
if hasattr(base, '_meta'):
|
if hasattr(base, '_meta'):
|
||||||
# Ensure that the Document class may be subclassed -
|
# Ensure that the Document class may be subclassed -
|
||||||
# inheritance may be disabled to remove dependency on
|
# inheritance may be disabled to remove dependency on
|
||||||
# additional fields _cls and _types
|
# additional fields _cls and _types
|
||||||
if base._meta.get('allow_inheritance', True) == False:
|
if base._meta.get('allow_inheritance', True) == False:
|
||||||
raise ValueError('Document %s may not be subclassed' %
|
raise ValueError('Document %s may not be subclassed' %
|
||||||
@ -190,7 +190,6 @@ class DocumentMetaclass(type):
|
|||||||
|
|
||||||
attrs['_class_name'] = '.'.join(reversed(class_name))
|
attrs['_class_name'] = '.'.join(reversed(class_name))
|
||||||
attrs['_superclasses'] = superclasses
|
attrs['_superclasses'] = superclasses
|
||||||
|
|
||||||
# Add the document's fields to the _fields attribute
|
# Add the document's fields to the _fields attribute
|
||||||
for attr_name, attr_value in attrs.items():
|
for attr_name, attr_value in attrs.items():
|
||||||
if hasattr(attr_value, "__class__") and \
|
if hasattr(attr_value, "__class__") and \
|
||||||
@ -211,12 +210,12 @@ class DocumentMetaclass(type):
|
|||||||
|
|
||||||
module = attrs.get('__module__')
|
module = attrs.get('__module__')
|
||||||
|
|
||||||
base_excs = tuple(base.DoesNotExist for base in bases
|
base_excs = tuple(base.DoesNotExist for base in bases
|
||||||
if hasattr(base, 'DoesNotExist')) or (DoesNotExist,)
|
if hasattr(base, 'DoesNotExist')) or (DoesNotExist,)
|
||||||
exc = subclass_exception('DoesNotExist', base_excs, module)
|
exc = subclass_exception('DoesNotExist', base_excs, module)
|
||||||
new_class.add_to_class('DoesNotExist', exc)
|
new_class.add_to_class('DoesNotExist', exc)
|
||||||
|
|
||||||
base_excs = tuple(base.MultipleObjectsReturned for base in bases
|
base_excs = tuple(base.MultipleObjectsReturned for base in bases
|
||||||
if hasattr(base, 'MultipleObjectsReturned'))
|
if hasattr(base, 'MultipleObjectsReturned'))
|
||||||
base_excs = base_excs or (MultipleObjectsReturned,)
|
base_excs = base_excs or (MultipleObjectsReturned,)
|
||||||
exc = subclass_exception('MultipleObjectsReturned', base_excs, module)
|
exc = subclass_exception('MultipleObjectsReturned', base_excs, module)
|
||||||
@ -238,9 +237,9 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
|
|
||||||
def __new__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
super_new = super(TopLevelDocumentMetaclass, cls).__new__
|
super_new = super(TopLevelDocumentMetaclass, cls).__new__
|
||||||
# Classes defined in this package are abstract and should not have
|
# Classes defined in this package are abstract and should not have
|
||||||
# their own metadata with DB collection, etc.
|
# their own metadata with DB collection, etc.
|
||||||
# __metaclass__ is only set on the class with the __metaclass__
|
# __metaclass__ is only set on the class with the __metaclass__
|
||||||
# attribute (i.e. it is not set on subclasses). This differentiates
|
# attribute (i.e. it is not set on subclasses). This differentiates
|
||||||
# 'real' documents from the 'Document' class
|
# 'real' documents from the 'Document' class
|
||||||
if attrs.get('__metaclass__') == TopLevelDocumentMetaclass:
|
if attrs.get('__metaclass__') == TopLevelDocumentMetaclass:
|
||||||
@ -366,7 +365,7 @@ class BaseDocument(object):
|
|||||||
are present.
|
are present.
|
||||||
"""
|
"""
|
||||||
# Get a list of tuples of field names and their current values
|
# Get a list of tuples of field names and their current values
|
||||||
fields = [(field, getattr(self, name))
|
fields = [(field, getattr(self, name))
|
||||||
for name, field in self._fields.items()]
|
for name, field in self._fields.items()]
|
||||||
|
|
||||||
# Ensure that each field is matched to a valid value
|
# Ensure that each field is matched to a valid value
|
||||||
|
@ -339,7 +339,7 @@ class ListField(BaseField):
|
|||||||
|
|
||||||
if isinstance(self.field, ReferenceField):
|
if isinstance(self.field, ReferenceField):
|
||||||
referenced_type = self.field.document_type
|
referenced_type = self.field.document_type
|
||||||
# Get value from document instance if available
|
# Get value from document instance if available
|
||||||
value_list = instance._data.get(self.name)
|
value_list = instance._data.get(self.name)
|
||||||
if value_list:
|
if value_list:
|
||||||
deref_list = []
|
deref_list = []
|
||||||
@ -643,7 +643,7 @@ class GridFSProxy(object):
|
|||||||
if not self.newfile:
|
if not self.newfile:
|
||||||
self.new_file()
|
self.new_file()
|
||||||
self.grid_id = self.newfile._id
|
self.grid_id = self.newfile._id
|
||||||
self.newfile.writelines(lines)
|
self.newfile.writelines(lines)
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
try:
|
try:
|
||||||
|
@ -523,6 +523,10 @@ class QuerySet(object):
|
|||||||
fields = []
|
fields = []
|
||||||
field = None
|
field = None
|
||||||
for field_name in parts:
|
for field_name in parts:
|
||||||
|
if field_name.isdigit():
|
||||||
|
fields.append(field_name)
|
||||||
|
field = field.field
|
||||||
|
continue
|
||||||
if field is None:
|
if field is None:
|
||||||
# Look up first field from the document
|
# Look up first field from the document
|
||||||
if field_name == 'pk':
|
if field_name == 'pk':
|
||||||
@ -620,7 +624,6 @@ class QuerySet(object):
|
|||||||
mongo_query[key] = value
|
mongo_query[key] = value
|
||||||
elif key in mongo_query and isinstance(mongo_query[key], dict):
|
elif key in mongo_query and isinstance(mongo_query[key], dict):
|
||||||
mongo_query[key].update(value)
|
mongo_query[key].update(value)
|
||||||
|
|
||||||
return mongo_query
|
return mongo_query
|
||||||
|
|
||||||
def get(self, *q_objs, **query):
|
def get(self, *q_objs, **query):
|
||||||
@ -1010,7 +1013,6 @@ class QuerySet(object):
|
|||||||
"""
|
"""
|
||||||
operators = ['set', 'unset', 'inc', 'dec', 'pop', 'push', 'push_all',
|
operators = ['set', 'unset', 'inc', 'dec', 'pop', 'push', 'push_all',
|
||||||
'pull', 'pull_all', 'add_to_set']
|
'pull', 'pull_all', 'add_to_set']
|
||||||
|
|
||||||
mongo_update = {}
|
mongo_update = {}
|
||||||
for key, value in update.items():
|
for key, value in update.items():
|
||||||
parts = key.split('__')
|
parts = key.split('__')
|
||||||
@ -1033,10 +1035,15 @@ class QuerySet(object):
|
|||||||
if _doc_cls:
|
if _doc_cls:
|
||||||
# Switch field names to proper names [set in Field(name='foo')]
|
# Switch field names to proper names [set in Field(name='foo')]
|
||||||
fields = QuerySet._lookup_field(_doc_cls, parts)
|
fields = QuerySet._lookup_field(_doc_cls, parts)
|
||||||
parts = [field.db_field for field in fields]
|
parts = []
|
||||||
|
for field in fields:
|
||||||
|
if isinstance(field, str):
|
||||||
|
parts.append(field)
|
||||||
|
else:
|
||||||
|
parts.append(field.db_field)
|
||||||
# Convert value to proper value
|
# Convert value to proper value
|
||||||
field = fields[-1]
|
field = fields[-1]
|
||||||
|
|
||||||
if op in (None, 'set', 'push', 'pull', 'addToSet'):
|
if op in (None, 'set', 'push', 'pull', 'addToSet'):
|
||||||
value = field.prepare_query_value(op, value)
|
value = field.prepare_query_value(op, value)
|
||||||
elif op in ('pushAll', 'pullAll'):
|
elif op in ('pushAll', 'pullAll'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user