Merge branch 'binary-fields' of git://github.com/benmur/mongoengine

This commit is contained in:
Harry Marr
2010-03-08 22:17:21 +00:00
2 changed files with 77 additions and 1 deletions

View File

@@ -11,7 +11,8 @@ import decimal
__all__ = ['StringField', 'IntField', 'FloatField', 'BooleanField',
'DateTimeField', 'EmbeddedDocumentField', 'ListField', 'DictField',
'ObjectIdField', 'ReferenceField', 'ValidationError',
'DecimalField', 'URLField', 'GenericReferenceField']
'DecimalField', 'URLField', 'GenericReferenceField',
'BinaryField']
RECURSIVE_REFERENCE_CONSTANT = 'self'
@@ -442,3 +443,23 @@ class GenericReferenceField(BaseField):
def prepare_query_value(self, op, value):
return self.to_mongo(value)['_ref']
class BinaryField(BaseField):
"""A binary data field.
"""
def __init__(self, max_bytes=None, **kwargs):
self.max_bytes = max_bytes
super(BinaryField, self).__init__(**kwargs)
def to_mongo(self, value):
return pymongo.binary.Binary(value)
def to_python(self, value):
return str(value)
def validate(self, value):
assert isinstance(value, str)
if self.max_bytes is not None and len(value) > self.max_bytes:
raise ValidationError('Binary value is too long')