Skip to content

Commit

Permalink
Disallowed empty strings for PickledObjectField.
Browse files Browse the repository at this point in the history
That enforces the usage of `null=True` to store empty values.
  • Loading branch information
cdman authored and charettes committed Nov 25, 2018
1 parent dfa4f6e commit 6313b66
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ Changes in version 2.0.0
* Silenced ``RemovedInDjango30Warning`` warnings on Django 2.0+ (thanks to
canarduck).
* Restructured project directories.
* Disallowed the usage of empty strings for ``PickledObjectField``. That makes
``.save()``, ``.create()``, etc. raise ``IntegrityError`` if `null` is not
``True`` and no default value was specified like built-in fields do
(thanks to Attila-Mihaly Balazs).

Changes in version 1.1.0
========================
Expand Down
1 change: 1 addition & 0 deletions picklefield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class PickledObjectField(models.Field):
can still do lookups using None). This way, it is still possible to
use the ``isnull`` lookup type correctly.
"""
empty_strings_allowed = False

def __init__(self, *args, **kwargs):
self.compress = kwargs.pop('compress', False)
Expand Down
7 changes: 6 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from datetime import date

from django.db import IntegrityError
from django.core import serializers
from django.test import TestCase
from picklefield.fields import dbsafe_encode, wrap_conflictual_object
Expand Down Expand Up @@ -37,7 +38,7 @@ def test_data_integrity(self):

# Make sure the default value for default_pickled_field gets stored
# correctly and that it isn't converted to a string.
model_test = TestingModel()
model_test = TestingModel(pickle_field=1, compressed_pickle_field=1)
model_test.save()
model_test = TestingModel.objects.get(id__exact=model_test.id)
self.assertEqual((D1, S1, T1, L1), model_test.default_pickle_field)
Expand Down Expand Up @@ -172,3 +173,7 @@ def test_no_copy(self):
compressed_pickle_field='Copy Me',
non_copying_field='Dont copy me'
)

def test_empty_strings_not_allowed(self):
with self.assertRaises(IntegrityError):
MinimalTestingModel.objects.create()

0 comments on commit 6313b66

Please sign in to comment.