django.db.backends.schema.BaseDatabaseSchemaEditor.effective_default (https://github.com/django/django/blob/1.7b4/django/db/backends/schema.py#L157) selects "" as the default value if none is given for a BinaryField. When you try to migrate for the first time, you'll get an error.
Temporary workaround
If you have a BinaryField that you want to have both null=True and editable=False, make sure to give it an explicit default of a bytes value such as b"".
Steps to reproduce
- Use Python 3.4.1 and Django 1.7b4.
- Make an empty project with a single empty app called a.
- Make a/models.py read as follows:
from django.db import models
class M(models.Model):
b = models.BinaryField(blank=True, editable=False)
- Add a to settings.py's INSTALLED_APPS
- ./manage.py makemigrations a
- ./manage.py migrate
You should see the following error:
Applying bt.0001_initial...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "venv3/lib/python3.4/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
utility.execute()
File "venv3/lib/python3.4/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "venv3/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "venv3/lib/python3.4/site-packages/django/core/management/base.py", line 337, in execute
output = self.handle(*args, **options)
File "venv3/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 146, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "venv3/lib/python3.4/site-packages/django/db/migrations/executor.py", line 62, in migrate
self.apply_migration(migration, fake=fake)
File "venv3/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
migration.apply(project_state, schema_editor)
File "venv3/lib/python3.4/site-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "venv3/lib/python3.4/site-packages/django/db/migrations/operations/models.py", line 30, in database_forwards
schema_editor.create_model(model)
File "venv3/lib/python3.4/site-packages/django/db/backends/schema.py", line 197, in create_model
definition, extra_params = self.column_sql(model, field)
File "venv3/lib/python3.4/site-packages/django/db/backends/schema.py", line 120, in column_sql
default_value = self.effective_default(field)
File "venv3/lib/python3.4/site-packages/django/db/backends/schema.py", line 172, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "venv3/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 625, in get_db_prep_save
prepared=False)
File "venv3/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 2023, in get_db_prep_value
return connection.Database.Binary(value)
TypeError: memoryview: str object does not have the buffer interface
Suggested fix
The aforementioned effective_default method should detect BinaryFields and if they allow empty strings, give them a default value of the empty bytes string rather than Unicode string. Specifically, this is probably a problem on Python 3 more so than Python 2.
Hi,
I can indeed reproduce the issue.
I'll bump the severity to release blocker as well.
Thanks for this quality bug report (nice format, easy steps to reproduce and a traceback: I wish all our reports were like this one)!