Raised when a key is defined in the structure spec but is missing from a data dictionary.
Raised when malformed document structure is detected.
Raised when a key in data dictionary is missing from the corresponding structure spec.
Raised when a document or its part cannot pass validation.
Validates given document against given structure specification. Always returns None.
Parameters: |
|
---|
Can raise:
Checks whether given document structure specification dictionary if defined correctly.
Raises StructureSpecificationError if the specification is malformed.
Default series of mergers:
Any value from spec that can be checked for type.
Nested dictionary. Example:
>>> DictMerger({'a': 123}, {}).process()
{'a': 123}
>>> DictMerger({'a': 123}, {'a': 456}).process()
{'a': 456}
Default value is obtained from a function with no arguments. It is expected that the callable does not have side effects. Example:
>>> FuncMerger(lambda: 123, None).process()
123
>>> FuncMerger(lambda: 123, 456).process()
456
Type definition. Preserves empty values. Example:
>>> TypeMerger(int, None).process()
None
>>> TypeMerger(int, 123).process()
123
Base class for value mergers.
Returns a merged value based on given spec and data, using given sequence of mergers.
The mergers are polled expected to be subclasses of ValueMerger. They are polled one by one; the first one that agrees to process given value is used to produce the result.
Example:
>>> merge_value({'a': 123}, {}, [DictMerger])
{'a': 123}
>>> merge_value({'a': 123}, {'a': 456}, [DictMerger])
{'a': 456}
Returns a dictionary based on spec + data.
Does not validate values. If data overrides a default value, it is trusted. The result can be validated later with validate_structure().
Note that a key/value pair is added from spec either if data does not define this key at all, or if the value is None. This behaviour may not be suitable for all cases and therefore may change in the future.
You can fine-tune the process by changing the list of mergers.
Parameters: |
|
---|
Let’s declare a model with indexes:
class Item(Document):
structure = dict(text=unicode, slug=unicode)
indexes = dict(text=None, slug=dict(unique=True))
Now create a model instance:
item = Item(text=u'foo', slug=u'bar')
Save it and make sure the indexes are created:
item.save(db)
The last line is roughly equivalent to:
collection = db[item.collection]
collection.ensure_index('text')
collection.ensure_index('slug', unique=True)
collection.save(dict(item)) # also validation, transformation, etc.
A structured dictionary that is bound to MongoDB and supports dot notation for access to items.
Inherits features from:
Makes the dictionary dot-expandable by exposing dictionary members via __getattr__ and __setattr__ in addition to __getitem__ and __setitem__. For example, this is the default API:
data = {'foo': {'bar': 0 } }
print data['foo']['bar']
data['foo']['bar'] = 123
This mixin adds the following API:
print data.foo.bar
data.foo.bar = 123
Nested dictionaries are converted to dot-expanded ones on adding.
Adds MongoDB-specific features to the dictionary.
Collection name.
(TODO)
Returns a MongoResultSet object.
Example:
items = Item.find(db, {'title': u'Hello'})
Note
The arguments are those of pymongo collection’s find method. A frequent error is to pass query key/value pairs as keyword arguments. This is wrong. In most cases you will want to pass a dictionary (“query spec”) as the first positional argument.
Returns an object that corresponds to given query or None.
Example:
item = Item.get_one(db, {'title': u'Hello'})
Saves the object to given database. Usage:
item = Item(title=u'Hello')
item.save(db)
Collection name is taken from MongoBoundDictMixin.collection.
A dictionary with structure specification and validation.
The document structure specification. For details see monk.validation.validate_structure_spec() and monk.validation.validate_structure().