Allow to add custom metadata to fields

This commit is contained in:
Vicky Donchenko 2015-06-01 17:57:40 +03:00
parent 33ea2b4844
commit 6bd9529a66
4 changed files with 23 additions and 2 deletions

View File

@ -225,4 +225,5 @@ that much better:
* Charanpal Dhanjal (https://github.com/charanpald)
* Emmanuel Leblond (https://github.com/touilleMan)
* Breeze.Kay (https://github.com/9nix00)
* Vicki Donchenko (https://github.com/kivistein)

View File

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

View File

@ -44,7 +44,7 @@ class BaseField(object):
def __init__(self, db_field=None, name=None, required=False, default=None,
unique=False, unique_with=None, primary_key=False,
validation=None, choices=None, verbose_name=None,
help_text=None, null=False, sparse=False):
help_text=None, null=False, sparse=False, custom_data=None):
"""
:param db_field: The database field to store this field in
(defaults to the name of the field)
@ -71,6 +71,7 @@ class BaseField(object):
then the default value is set
:param sparse: (optional) `sparse=True` combined with `unique=True` and `required=False`
means that uniqueness won't be enforced for `None` values
:param custom_data: (optional) Custom metadata for this field.
"""
self.db_field = (db_field or name) if not primary_key else '_id'
@ -89,6 +90,7 @@ class BaseField(object):
self.null = null
self.sparse = sparse
self._owner_document = None
self.custom_data = custom_data
# Adjust the appropriate creation counter, and save our local copy.
if self.db_field == '_id':

View File

@ -3818,5 +3818,22 @@ class EmbeddedDocumentListFieldTestCase(unittest.TestCase):
# deleted from the database
self.assertEqual(number, 1)
def test_custom_data(self):
"""
Tests that custom data is saved in the field object
and doesn't interfere with the rest of field functionalities.
"""
custom_data = {'a': 'a_value', 'b': [1, 2]}
class CustomData(Document):
a_field = IntField()
c_field = IntField(custom_data=custom_data)
a1 = CustomData(a_field=1, c_field=2).save()
self.assertEqual(2, a1.c_field)
self.assertFalse(hasattr(a1.c_field, 'custom_data'))
self.assertTrue(hasattr(CustomData.c_field, 'custom_data'))
self.assertEqual(custom_data['a'], CustomData.c_field.custom_data['a'])
if __name__ == '__main__':
unittest.main()