#28431: default='' (non-bytestring) on BinaryField crashes some migration
operations
----------------------------+----------------------------------------
     Reporter:  James       |                    Owner:  Windson yang
         Type:  Bug         |                   Status:  assigned
    Component:  Migrations  |                  Version:  1.10
     Severity:  Normal      |               Resolution:
     Keywords:              |             Triage Stage:  Accepted
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+----------------------------------------

Comment (by Claude Paroz):

 I would also suggest a system check to prevent default strings in the
 first place. Something like:
 {{{
 diff --git a/django/db/models/fields/__init__.py
 b/django/db/models/fields/__init__.py
 index b2e9b18351..af4671c0f6 100644
 --- a/django/db/models/fields/__init__.py
 +++ b/django/db/models/fields/__init__.py
 @@ -2292,6 +2292,22 @@ class BinaryField(Field):
          if self.max_length is not None:
 self.validators.append(validators.MaxLengthValidator(self.max_length))

 +    def check(self, **kwargs):
 +        errors = super().check(**kwargs)
 +        errors.extend(self._check_default_is_not_str(**kwargs))
 +        return errors
 +
 +    def _check_default_is_not_str(self, **kwargs):
 +        if self.has_default() and isinstance(self.default, str):
 +            return [
 +                checks.Error(
 +                    "BinaryField 'default' cannot be a string, use bytes
 content instead.",
 +                    obj=self,
 +                    id='fields.E170',
 +                )
 +            ]
 +        return []
 +
      def deconstruct(self):
          name, path, args, kwargs = super().deconstruct()
          del kwargs['editable']
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28431#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.96417e57c7e70780ad809077d7db5802%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to