Skip to content

Commit 4c80154

Browse files
committed
Merge pull request #1014 from kivistein/fix-705
Allow to add custom metadata to fields
2 parents 33ea2b4 + 6bd9529 commit 4c80154

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,5 @@ that much better:
225225
* Charanpal Dhanjal (https://github.com/charanpald)
226226
* Emmanuel Leblond (https://github.com/touilleMan)
227227
* Breeze.Kay (https://github.com/9nix00)
228+
* Vicki Donchenko (https://github.com/kivistein)
228229

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Changes in 0.9.X - DEV
1919
- Fix for updating sorting in SortedListField. #978
2020
- Added __ support to escape field name in fields lookup keywords that match operators names #949
2121
- Support for PyMongo 3+ #946
22-
- Fix for issue where FileField deletion did not free space in GridFS.
22+
- Fix for issue where FileField deletion did not free space in GridFS.
2323
- No_dereference() not respected on embedded docs containing reference. #517
2424
- Document save raise an exception if save_condition fails #1005
2525
- Fixes some internal _id handling issue. #961
@@ -30,6 +30,7 @@ Changes in 0.9.X - DEV
3030
- Fix for delete with write_concern {'w': 0}. #1008
3131
- Allow dynamic lookup for more than two parts. #882
3232
- Added support for min_distance on geo queries. #831
33+
- Allow to add custom metadata to fields #705
3334
3435
Changes in 0.9.0
3536
================

mongoengine/base/fields.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class BaseField(object):
4444
def __init__(self, db_field=None, name=None, required=False, default=None,
4545
unique=False, unique_with=None, primary_key=False,
4646
validation=None, choices=None, verbose_name=None,
47-
help_text=None, null=False, sparse=False):
47+
help_text=None, null=False, sparse=False, custom_data=None):
4848
"""
4949
:param db_field: The database field to store this field in
5050
(defaults to the name of the field)
@@ -71,6 +71,7 @@ def __init__(self, db_field=None, name=None, required=False, default=None,
7171
then the default value is set
7272
:param sparse: (optional) `sparse=True` combined with `unique=True` and `required=False`
7373
means that uniqueness won't be enforced for `None` values
74+
:param custom_data: (optional) Custom metadata for this field.
7475
"""
7576
self.db_field = (db_field or name) if not primary_key else '_id'
7677

@@ -89,6 +90,7 @@ def __init__(self, db_field=None, name=None, required=False, default=None,
8990
self.null = null
9091
self.sparse = sparse
9192
self._owner_document = None
93+
self.custom_data = custom_data
9294

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

tests/fields/fields.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,5 +3818,22 @@ def test_filtered_delete(self):
38183818
# deleted from the database
38193819
self.assertEqual(number, 1)
38203820

3821+
def test_custom_data(self):
3822+
"""
3823+
Tests that custom data is saved in the field object
3824+
and doesn't interfere with the rest of field functionalities.
3825+
"""
3826+
custom_data = {'a': 'a_value', 'b': [1, 2]}
3827+
class CustomData(Document):
3828+
a_field = IntField()
3829+
c_field = IntField(custom_data=custom_data)
3830+
3831+
a1 = CustomData(a_field=1, c_field=2).save()
3832+
self.assertEqual(2, a1.c_field)
3833+
self.assertFalse(hasattr(a1.c_field, 'custom_data'))
3834+
self.assertTrue(hasattr(CustomData.c_field, 'custom_data'))
3835+
self.assertEqual(custom_data['a'], CustomData.c_field.custom_data['a'])
3836+
3837+
38213838
if __name__ == '__main__':
38223839
unittest.main()

0 commit comments

Comments
 (0)