summaryrefslogtreecommitdiff
path: root/django/db/models/fields/__init__.py
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-12-13 22:11:06 +0100
committerClaude Paroz <claude@2xlibre.net>2013-03-02 10:29:02 +0100
commit8ee1eddb7e148de89aebde9e68da495633fc1ec9 (patch)
tree36b83db0cf15b0be47557449b9ed056646099542 /django/db/models/fields/__init__.py
parent0f306cad8442f9808e4410f190f434144ff6376a (diff)
Add a BinaryField model field
Thanks Michael Jung, Charl Botha and Florian Apolloner for review and help on the patch.
Diffstat (limited to 'django/db/models/fields/__init__.py')
-rw-r--r--django/db/models/fields/__init__.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 2702876397..8302aaceaa 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -1291,3 +1291,30 @@ class URLField(CharField):
}
defaults.update(kwargs)
return super(URLField, self).formfield(**defaults)
+
+class BinaryField(Field):
+ description = _("Raw binary data")
+
+ def __init__(self, *args, **kwargs):
+ kwargs['editable'] = False
+ super(BinaryField, self).__init__(*args, **kwargs)
+ if self.max_length is not None:
+ self.validators.append(validators.MaxLengthValidator(self.max_length))
+
+ def get_internal_type(self):
+ return "BinaryField"
+
+ def get_default(self):
+ if self.has_default() and not callable(self.default):
+ return self.default
+ default = super(BinaryField, self).get_default()
+ if default == '':
+ return b''
+ return default
+
+ def get_db_prep_value(self, value, connection, prepared=False):
+ value = super(BinaryField, self
+ ).get_db_prep_value(value, connection, prepared)
+ if value is not None:
+ return connection.Database.Binary(value)
+ return value