Merge pull request #1129 from illico/feature/arbitrary-metadata

Indirection-free optimized field metadata.
This commit is contained in:
Omer Katz 2015-11-23 12:49:48 +02:00
commit c0e7f341cb
2 changed files with 22 additions and 16 deletions

View File

@ -172,11 +172,11 @@ arguments can be set on all fields:
class Shirt(Document): class Shirt(Document):
size = StringField(max_length=3, choices=SIZE) size = StringField(max_length=3, choices=SIZE)
:attr:`help_text` (Default: None) :attr:`**kwargs` (Optional)
Optional help text to output with the field -- used by form libraries You can supply additional metadata as arbitrary additional keyword
arguments. You can not override existing attributes, however. Common
:attr:`verbose_name` (Default: None) choices include `help_text` and `verbose_name`, commonly used by form and
Optional human-readable name for the field -- used by form libraries widget libraries.
List fields List fields

View File

@ -41,8 +41,8 @@ class BaseField(object):
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, verbose_name=None, validation=None, choices=None, null=False, sparse=False,
help_text=None, null=False, sparse=False, custom_data=None): **kwargs):
""" """
: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)
@ -60,16 +60,15 @@ class BaseField(object):
field. Generally this is deprecated in favour of the field. Generally this is deprecated in favour of the
`FIELD.validate` method `FIELD.validate` method
:param choices: (optional) The valid choices :param choices: (optional) The valid choices
:param verbose_name: (optional) The verbose name for the field.
Designed to be human readable and is often used when generating
model forms from the document model.
:param help_text: (optional) The help text for this field and is often
used when generating model forms from the document model.
:param null: (optional) Is the field value can be null. If no and there is a default value :param null: (optional) Is the field value can be null. If no and there is a default value
then the default value is set then the default value is set
:param sparse: (optional) `sparse=True` combined with `unique=True` and `required=False` :param sparse: (optional) `sparse=True` combined with `unique=True` and `required=False`
means that uniqueness won't be enforced for `None` values means that uniqueness won't be enforced for `None` values
:param custom_data: (optional) Custom metadata for this field. :param **kwargs: (optional) Arbitrary indirection-free metadata for
this field can be supplied as additional keyword arguments and
accessed as attributes of the field. Must not conflict with any
existing attributes. Common metadata includes `verbose_name` and
`help_text`.
""" """
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'
@ -83,12 +82,19 @@ class BaseField(object):
self.primary_key = primary_key self.primary_key = primary_key
self.validation = validation self.validation = validation
self.choices = choices self.choices = choices
self.verbose_name = verbose_name
self.help_text = help_text
self.null = null self.null = null
self.sparse = sparse self.sparse = sparse
self._owner_document = None self._owner_document = None
self.custom_data = custom_data
# Detect and report conflicts between metadata and base properties.
conflicts = set(dir(self)) & set(kwargs)
if conflicts:
raise TypeError("%s already has attribute(s): %s" % (
self.__class__.__name__, ', '.join(conflicts) ))
# Assign metadata to the instance
# This efficient method is available because no __slots__ are defined.
self.__dict__.update(kwargs)
# Adjust the appropriate creation counter, and save our local copy. # Adjust the appropriate creation counter, and save our local copy.
if self.db_field == '_id': if self.db_field == '_id':