From fb2964a4106b1282c4179b6fbbd0374f5be1ccac Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Wed, 26 Sep 2018 11:38:12 -0700 Subject: [PATCH] Added test of filtering on BinaryField and corrected docs. --- docs/ref/models/fields.txt | 5 ++--- tests/model_fields/test_binaryfield.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index f25658ae70..60d6fd5fc9 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -416,9 +416,8 @@ guaranteed to fit numbers from ``-9223372036854775808`` to .. class:: BinaryField(max_length=None, **options) -A field to store raw binary data. It only supports ``bytes`` assignment. Be -aware that this field has limited functionality. For example, it is not possible -to filter a queryset on a ``BinaryField`` value. +A field to store raw binary data. It can be assigned :class:`bytes` or a +:class:`memoryview`. By default, ``BinaryField`` sets :attr:`~Field.editable` to ``False``, in which case it can't be included in a :class:`~django.forms.ModelForm`. diff --git a/tests/model_fields/test_binaryfield.py b/tests/model_fields/test_binaryfield.py index ee40ed48fb..9865f131d3 100644 --- a/tests/model_fields/test_binaryfield.py +++ b/tests/model_fields/test_binaryfield.py @@ -34,3 +34,13 @@ class BinaryFieldTests(TestCase): self.assertIs(field.editable, True) field = models.BinaryField(editable=False) self.assertIs(field.editable, False) + + def test_filter(self): + dm = DataModel.objects.create(data=self.binary_data) + DataModel.objects.create(data=b'\xef\xbb\xbf') + self.assertSequenceEqual(DataModel.objects.filter(data=self.binary_data), [dm]) + + def test_filter_memoryview(self): + dm = DataModel.objects.create(data=self.binary_data) + DataModel.objects.create(data=b'\xef\xbb\xbf') + self.assertSequenceEqual(DataModel.objects.filter(data=memoryview(self.binary_data)), [dm])