Deprecated 'name' arg for fields in favour of 'db_field'
This commit is contained in:
@@ -21,9 +21,14 @@ class BaseField(object):
|
||||
# Fields may have _types inserted into indexes by default
|
||||
_index_with_types = True
|
||||
|
||||
def __init__(self, name=None, required=False, default=None, unique=False,
|
||||
unique_with=None, primary_key=False):
|
||||
self.name = name if not primary_key else '_id'
|
||||
def __init__(self, db_field=None, name=None, required=False, default=None,
|
||||
unique=False, unique_with=None, primary_key=False):
|
||||
self.db_field = (db_field or name) if not primary_key else '_id'
|
||||
if name:
|
||||
import warnings
|
||||
msg = "Fields' 'name' attribute deprecated in favour of 'db_field'"
|
||||
warnings.warn(msg, DeprecationWarning)
|
||||
self.name = None
|
||||
self.required = required or primary_key
|
||||
self.default = default
|
||||
self.unique = bool(unique or unique_with)
|
||||
@@ -151,9 +156,10 @@ class DocumentMetaclass(type):
|
||||
# Add the document's fields to the _fields attribute
|
||||
for attr_name, attr_value in attrs.items():
|
||||
if hasattr(attr_value, "__class__") and \
|
||||
issubclass(attr_value.__class__, BaseField):
|
||||
if not attr_value.name:
|
||||
attr_value.name = attr_name
|
||||
issubclass(attr_value.__class__, BaseField):
|
||||
attr_value.name = attr_name
|
||||
if not attr_value.db_field:
|
||||
attr_value.db_field = attr_name
|
||||
doc_fields[attr_name] = attr_value
|
||||
attrs['_fields'] = doc_fields
|
||||
|
||||
@@ -234,7 +240,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
parts = other_name.split('.')
|
||||
# Lookup real name
|
||||
parts = QuerySet._lookup_field(new_class, parts)
|
||||
name_parts = [part.name for part in parts]
|
||||
name_parts = [part.db_field for part in parts]
|
||||
unique_with.append('.'.join(name_parts))
|
||||
# Unique field should be required
|
||||
parts[-1].required = True
|
||||
@@ -258,7 +264,8 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
|
||||
if not new_class._meta['id_field']:
|
||||
new_class._meta['id_field'] = 'id'
|
||||
new_class.id = new_class._fields['id'] = ObjectIdField(name='_id')
|
||||
new_class._fields['id'] = ObjectIdField(db_field='_id')
|
||||
new_class.id = new_class._fields['id']
|
||||
|
||||
_document_registry[name] = new_class
|
||||
|
||||
@@ -362,7 +369,7 @@ class BaseDocument(object):
|
||||
for field_name, field in self._fields.items():
|
||||
value = getattr(self, field_name, None)
|
||||
if value is not None:
|
||||
data[field.name] = field.to_mongo(value)
|
||||
data[field.db_field] = field.to_mongo(value)
|
||||
# Only add _cls and _types if allow_inheritance is not False
|
||||
if not (hasattr(self, '_meta') and
|
||||
self._meta.get('allow_inheritance', True) == False):
|
||||
@@ -398,8 +405,8 @@ class BaseDocument(object):
|
||||
present_fields = data.keys()
|
||||
|
||||
for field_name, field in cls._fields.items():
|
||||
if field.name in data:
|
||||
data[field_name] = field.to_python(data[field.name])
|
||||
if field.db_field in data:
|
||||
data[field_name] = field.to_python(data[field.db_field])
|
||||
|
||||
obj = cls(**data)
|
||||
obj._present_fields = present_fields
|
||||
|
||||
@@ -328,7 +328,7 @@ class DictField(BaseField):
|
||||
'contain "." or "$" characters')
|
||||
|
||||
def lookup_member(self, member_name):
|
||||
return BaseField(name=member_name)
|
||||
return BaseField(db_field=member_name)
|
||||
|
||||
|
||||
class ReferenceField(BaseField):
|
||||
|
||||
@@ -186,7 +186,7 @@ class QuerySet(object):
|
||||
# objects for the next part (list field checking)
|
||||
parts = key.split('.')
|
||||
fields = QuerySet._lookup_field(doc_cls, parts)
|
||||
parts = [field.name for field in fields]
|
||||
parts = [field.db_field for field in fields]
|
||||
key = '.'.join(parts)
|
||||
index_list.append((key, direction))
|
||||
|
||||
@@ -289,7 +289,7 @@ class QuerySet(object):
|
||||
"""Translate a field attribute name to a database field name.
|
||||
"""
|
||||
parts = field.split(sep)
|
||||
parts = [f.name for f in QuerySet._lookup_field(doc_cls, parts)]
|
||||
parts = [f.db_field for f in QuerySet._lookup_field(doc_cls, parts)]
|
||||
return '.'.join(parts)
|
||||
|
||||
@classmethod
|
||||
@@ -312,7 +312,7 @@ class QuerySet(object):
|
||||
if _doc_cls:
|
||||
# Switch field names to proper names [set in Field(name='foo')]
|
||||
fields = QuerySet._lookup_field(_doc_cls, parts)
|
||||
parts = [field.name for field in fields]
|
||||
parts = [field.db_field for field in fields]
|
||||
|
||||
# Convert value to proper value
|
||||
field = fields[-1]
|
||||
@@ -567,8 +567,8 @@ class QuerySet(object):
|
||||
raise InvalidQueryError('Subfields cannot be used as '
|
||||
'arguments to QuerySet.only')
|
||||
# Translate field name
|
||||
field_name = QuerySet._lookup_field(self._document, field)[-1].name
|
||||
self._loaded_fields.append(field_name)
|
||||
field = QuerySet._lookup_field(self._document, field)[-1].db_field
|
||||
self._loaded_fields.append(field)
|
||||
|
||||
# _cls is needed for polymorphism
|
||||
if self._document._meta.get('allow_inheritance'):
|
||||
@@ -643,7 +643,7 @@ class QuerySet(object):
|
||||
if _doc_cls:
|
||||
# Switch field names to proper names [set in Field(name='foo')]
|
||||
fields = QuerySet._lookup_field(_doc_cls, parts)
|
||||
parts = [field.name for field in fields]
|
||||
parts = [field.db_field for field in fields]
|
||||
|
||||
# Convert value to proper value
|
||||
field = fields[-1]
|
||||
@@ -721,7 +721,7 @@ class QuerySet(object):
|
||||
field_name = match.group(1).split('.')
|
||||
fields = QuerySet._lookup_field(self._document, field_name)
|
||||
# Substitute the correct name for the field into the javascript
|
||||
return '["%s"]' % fields[-1].name
|
||||
return '["%s"]' % fields[-1].db_field
|
||||
|
||||
return re.sub('\[\s*~([A-z_][A-z_0-9.]+?)\s*\]', field_sub, code)
|
||||
|
||||
@@ -735,7 +735,7 @@ class QuerySet(object):
|
||||
options specified as keyword arguments.
|
||||
|
||||
As fields in MongoEngine may use different names in the database (set
|
||||
using the :attr:`name` keyword argument to a :class:`Field`
|
||||
using the :attr:`db_field` keyword argument to a :class:`Field`
|
||||
constructor), a mechanism exists for replacing MongoEngine field names
|
||||
with the database field names in Javascript code. When accessing a
|
||||
field, use square-bracket notation, and prefix the MongoEngine field
|
||||
|
||||
Reference in New Issue
Block a user