Fixed #2417 -- Add a BinaryField #597

Closed
wants to merge 3 commits into
from

Projects

None yet

5 participants

@claudep
Member
claudep commented Dec 17, 2012

No description provided.

@alex alex and 1 other commented on an outdated diff Dec 17, 2012
django/db/models/fields/__init__.py
+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):
+ default = super(BinaryField, self).get_default()
+ if default == '':
+ return b''
@alex
alex Dec 17, 2012 Django member

This should be returning the default value.

@claudep
claudep Dec 18, 2012 Django member

Of course! I'll also test it (and just noticed there is a problem with force_text in the default method).

@alex alex and 1 other commented on an outdated diff Dec 17, 2012
tests/regressiontests/serializers_regress/tests.py
@@ -115,9 +116,15 @@ def inherited_create(pk, klass, data):
# test data objects of various kinds
def data_compare(testcase, pk, klass, data):
instance = klass.objects.get(id=pk)
+ if klass.__name__ == 'BinaryData' and data is not None:
@alex
alex Dec 17, 2012 Django member

Why is this checking by name?

@claudep
claudep Dec 18, 2012 Django member

No reason, will fix...

@alex alex and 1 other commented on an outdated diff Dec 17, 2012
django/db/backends/__init__.py
@@ -854,6 +854,15 @@ def value_to_db_decimal(self, value, max_digits, decimal_places):
return None
return util.format_number(value, max_digits, decimal_places)
+ def value_to_db_binary(self, value):
+ """
+ Transform a byte-type value to an object compatible with what is expected
+ by the backend driver for binary/blob columns.
+ """
+ if value is not None and not isinstance(value, six.memoryview):
+ return six.memoryview(value)
+ return value
@alex
alex Dec 17, 2012 Django member

This looks like it's the same for every backend, why is this not just in the field?

@claudep
claudep Dec 18, 2012 Django member

Right, this is a remainder of the history of my patch. Will fix.

@bikeshedder

Using this in production with a MySQL and PostgreSQL database. Works great. Thanks! 👍

@claudep
Member
claudep commented Feb 26, 2013

Thanks, that's the first feedback about this code being used, so I do appreciate it! I'll try to push for inclusion in Django 1.6.

@apollo13
Member

As said on IRC: I don't have any use for it currently so it's just plain code review, but it looks good to me. I'd rather commit it early in 1.6 and fix later if people find issues…

@cpbotha cpbotha and 1 other commented on an outdated diff Mar 1, 2013
django/db/models/fields/__init__.py
+ 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 and not isinstance(value, six.memoryview):
+ return six.memoryview(value)
@cpbotha
cpbotha Mar 1, 2013

The Python DBAPI supplies the Binary() function for exactly this purpose: http://www.python.org/dev/peps/pep-0249/#binary

It also seems this is how sqlalchemy does its conversion of binary data for the backend: http://docs.sqlalchemy.org/en/rel_0_8/core/types.html#sqlalchemy.types.LargeBinary

Would that be usable here too, or have I misunderstood?

@claudep
claudep Mar 1, 2013 Django member

Thanks for the suggestion, seems to work well.

claudep added some commits Dec 13, 2012
@claudep claudep closed this May 24, 2013
@nanuxbe nanuxbe pushed a commit to nanuxbe/django that referenced this pull request Jul 2, 2016
@timgraham timgraham Fixed #597 -- Added a confirmation email for corporate member signups. 0eff588
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment