Add arbitrary metadata capture to BaseField.
				
					
				
			Includes ability to detect and report conflicts.
This commit is contained in:
		| @@ -42,7 +42,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, verbose_name=None, | ||||||
|                  help_text=None, null=False, sparse=False, custom_data=None): |                  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) | ||||||
| @@ -70,6 +71,7 @@ class BaseField(object): | |||||||
|         :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 custom_data: (optional) Custom metadata for this field. | ||||||
|  |         :param **kwargs: (optional) Arbitrary indirection-free metadata for this field. | ||||||
|         """ |         """ | ||||||
|         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' | ||||||
|  |  | ||||||
| @@ -90,6 +92,15 @@ class BaseField(object): | |||||||
|         self._owner_document = None |         self._owner_document = None | ||||||
|         self.custom_data = custom_data |         self.custom_data = custom_data | ||||||
|          |          | ||||||
|  |         conflicts = set(dir(self)).intersect(kwargs) | ||||||
|  |         if conflicts: | ||||||
|  |             raise TypeError("%s already has attribute(s): %s" % ( | ||||||
|  |                 self.__class__.__name__, | ||||||
|  |                 ', '.join(conflicts) | ||||||
|  |             )) | ||||||
|  |              | ||||||
|  |         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': | ||||||
|             self.creation_counter = BaseField.auto_creation_counter |             self.creation_counter = BaseField.auto_creation_counter | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user