diff options
| author | Hasan Ramezani <hasan.r67@gmail.com> | 2019-03-17 21:47:21 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-03-25 20:04:35 +0100 |
| commit | 981dd6dd71ea80e5149c2eff564622e96c12b5be (patch) | |
| tree | cf778f7862805def6a46f9ffd29e8a2dde7d1de7 /django | |
| parent | dc53f2135b341ef0b6203ecb0a1894cdbd174a9c (diff) | |
Fixed #28431 -- Added a system check for BinaryField to prevent strings defaults.
Thanks Claude Paroz for the initial patch.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/fields/__init__.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 71005490f3..2307dcae25 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -2252,6 +2252,21 @@ class BinaryField(Field): if self.max_length is not None: self.validators.append(validators.MaxLengthValidator(self.max_length)) + def check(self, **kwargs): + return [*super().check(**kwargs), *self._check_str_default_value()] + + def _check_str_default_value(self): + if self.has_default() and isinstance(self.default, str): + return [ + checks.Error( + "BinaryField's default cannot be a string. Use bytes " + "content instead.", + obj=self, + id='fields.E170', + ) + ] + return [] + def deconstruct(self): name, path, args, kwargs = super().deconstruct() if self.editable: |
