Skip to content

Commit c0e7f34

Browse files
author
Omer Katz
committed
Merge pull request #1129 from illico/feature/arbitrary-metadata
Indirection-free optimized field metadata.
2 parents b708dab + 50b271c commit c0e7f34

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

docs/guide/defining-documents.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ arguments can be set on all fields:
172172
class Shirt(Document):
173173
size = StringField(max_length=3, choices=SIZE)
174174

175-
:attr:`help_text` (Default: None)
176-
Optional help text to output with the field -- used by form libraries
177-
178-
:attr:`verbose_name` (Default: None)
179-
Optional human-readable name for the field -- used by form libraries
175+
:attr:`**kwargs` (Optional)
176+
You can supply additional metadata as arbitrary additional keyword
177+
arguments. You can not override existing attributes, however. Common
178+
choices include `help_text` and `verbose_name`, commonly used by form and
179+
widget libraries.
180180

181181

182182
List fields

mongoengine/base/fields.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class BaseField(object):
4141

4242
def __init__(self, db_field=None, name=None, required=False, default=None,
4343
unique=False, unique_with=None, primary_key=False,
44-
validation=None, choices=None, verbose_name=None,
45-
help_text=None, null=False, sparse=False, custom_data=None):
44+
validation=None, choices=None, null=False, sparse=False,
45+
**kwargs):
4646
"""
4747
:param db_field: The database field to store this field in
4848
(defaults to the name of the field)
@@ -60,16 +60,15 @@ def __init__(self, db_field=None, name=None, required=False, default=None,
6060
field. Generally this is deprecated in favour of the
6161
`FIELD.validate` method
6262
:param choices: (optional) The valid choices
63-
:param verbose_name: (optional) The verbose name for the field.
64-
Designed to be human readable and is often used when generating
65-
model forms from the document model.
66-
:param help_text: (optional) The help text for this field and is often
67-
used when generating model forms from the document model.
6863
:param null: (optional) Is the field value can be null. If no and there is a default value
6964
then the default value is set
7065
:param sparse: (optional) `sparse=True` combined with `unique=True` and `required=False`
7166
means that uniqueness won't be enforced for `None` values
72-
:param custom_data: (optional) Custom metadata for this field.
67+
:param **kwargs: (optional) Arbitrary indirection-free metadata for
68+
this field can be supplied as additional keyword arguments and
69+
accessed as attributes of the field. Must not conflict with any
70+
existing attributes. Common metadata includes `verbose_name` and
71+
`help_text`.
7372
"""
7473
self.db_field = (db_field or name) if not primary_key else '_id'
7574

@@ -83,12 +82,19 @@ def __init__(self, db_field=None, name=None, required=False, default=None,
8382
self.primary_key = primary_key
8483
self.validation = validation
8584
self.choices = choices
86-
self.verbose_name = verbose_name
87-
self.help_text = help_text
8885
self.null = null
8986
self.sparse = sparse
9087
self._owner_document = None
91-
self.custom_data = custom_data
88+
89+
# Detect and report conflicts between metadata and base properties.
90+
conflicts = set(dir(self)) & set(kwargs)
91+
if conflicts:
92+
raise TypeError("%s already has attribute(s): %s" % (
93+
self.__class__.__name__, ', '.join(conflicts) ))
94+
95+
# Assign metadata to the instance
96+
# This efficient method is available because no __slots__ are defined.
97+
self.__dict__.update(kwargs)
9298

9399
# Adjust the appropriate creation counter, and save our local copy.
94100
if self.db_field == '_id':

0 commit comments

Comments
 (0)