summaryrefslogtreecommitdiff
path: root/django
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
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')
-rw-r--r--django/db/backends/mysql/creation.py1
-rw-r--r--django/db/backends/oracle/creation.py1
-rw-r--r--django/db/backends/postgresql_psycopg2/creation.py1
-rw-r--r--django/db/backends/sqlite3/creation.py1
-rw-r--r--django/db/models/fields/__init__.py27
-rw-r--r--django/utils/six.py4
6 files changed, 35 insertions, 0 deletions
diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
index 01efe8d35b..3a57c29479 100644
--- a/django/db/backends/mysql/creation.py
+++ b/django/db/backends/mysql/creation.py
@@ -7,6 +7,7 @@ class DatabaseCreation(BaseDatabaseCreation):
# If a column type is set to None, it won't be included in the output.
data_types = {
'AutoField': 'integer AUTO_INCREMENT',
+ 'BinaryField': 'longblob',
'BooleanField': 'bool',
'CharField': 'varchar(%(max_length)s)',
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
diff --git a/django/db/backends/oracle/creation.py b/django/db/backends/oracle/creation.py
index 1cc3957232..aaca74e8d1 100644
--- a/django/db/backends/oracle/creation.py
+++ b/django/db/backends/oracle/creation.py
@@ -17,6 +17,7 @@ class DatabaseCreation(BaseDatabaseCreation):
data_types = {
'AutoField': 'NUMBER(11)',
+ 'BinaryField': 'BLOB',
'BooleanField': 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))',
'CharField': 'NVARCHAR2(%(max_length)s)',
'CommaSeparatedIntegerField': 'VARCHAR2(%(max_length)s)',
diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
index d977939f41..b19926b440 100644
--- a/django/db/backends/postgresql_psycopg2/creation.py
+++ b/django/db/backends/postgresql_psycopg2/creation.py
@@ -11,6 +11,7 @@ class DatabaseCreation(BaseDatabaseCreation):
# If a column type is set to None, it won't be included in the output.
data_types = {
'AutoField': 'serial',
+ 'BinaryField': 'bytea',
'BooleanField': 'boolean',
'CharField': 'varchar(%(max_length)s)',
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py
index 9dacac72e1..c90a697e35 100644
--- a/django/db/backends/sqlite3/creation.py
+++ b/django/db/backends/sqlite3/creation.py
@@ -9,6 +9,7 @@ class DatabaseCreation(BaseDatabaseCreation):
# schema inspection is more useful.
data_types = {
'AutoField': 'integer',
+ 'BinaryField': 'BLOB',
'BooleanField': 'bool',
'CharField': 'varchar(%(max_length)s)',
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
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
diff --git a/django/utils/six.py b/django/utils/six.py
index b93dc5b164..208c5c1112 100644
--- a/django/utils/six.py
+++ b/django/utils/six.py
@@ -394,10 +394,14 @@ if PY3:
_iterlists = "lists"
_assertRaisesRegex = "assertRaisesRegex"
_assertRegex = "assertRegex"
+ memoryview = memoryview
else:
_iterlists = "iterlists"
_assertRaisesRegex = "assertRaisesRegexp"
_assertRegex = "assertRegexpMatches"
+ # memoryview and buffer are not stricly equivalent, but should be fine for
+ # django core usage (mainly BinaryField)
+ memoryview = buffer
def iterlists(d):