Kaydet (Commit) b22917bd authored tarafından Andrew Godwin's avatar Andrew Godwin

Fixed #22851: BinaryView wasn't getting a binary default

üst 82c935d4
...@@ -8,7 +8,7 @@ from django.db.transaction import atomic ...@@ -8,7 +8,7 @@ from django.db.transaction import atomic
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from django.utils.log import getLogger from django.utils.log import getLogger
from django.utils.six.moves import reduce from django.utils.six.moves import reduce
from django.utils.six import callable from django.utils import six
logger = getLogger('django.db.backends.schema') logger = getLogger('django.db.backends.schema')
...@@ -169,11 +169,14 @@ class BaseDatabaseSchemaEditor(object): ...@@ -169,11 +169,14 @@ class BaseDatabaseSchemaEditor(object):
if field.has_default(): if field.has_default():
default = field.get_default() default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed: elif not field.null and field.blank and field.empty_strings_allowed:
default = "" if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
else: else:
default = None default = None
# If it's a callable, call it # If it's a callable, call it
if callable(default): if six.callable(default):
default = default() default = default()
# Run it through the field's get_db_prep_save method so we can send it # Run it through the field's get_db_prep_save method so we can send it
# to the database. # to the database.
......
...@@ -4,9 +4,10 @@ import unittest ...@@ -4,9 +4,10 @@ import unittest
from django.test import TransactionTestCase from django.test import TransactionTestCase
from django.db import connection, DatabaseError, IntegrityError, OperationalError from django.db import connection, DatabaseError, IntegrityError, OperationalError
from django.db.models.fields import IntegerField, TextField, CharField, SlugField, BooleanField from django.db.models.fields import IntegerField, TextField, CharField, SlugField, BooleanField, BinaryField
from django.db.models.fields.related import ManyToManyField, ForeignKey from django.db.models.fields.related import ManyToManyField, ForeignKey
from django.db.transaction import atomic from django.db.transaction import atomic
from django.utils import six
from .models import (Author, AuthorWithM2M, Book, BookWithLongName, from .models import (Author, AuthorWithM2M, Book, BookWithLongName,
BookWithSlug, BookWithM2M, Tag, TagIndexed, TagM2MTest, TagUniqueRename, BookWithSlug, BookWithM2M, Tag, TagIndexed, TagM2MTest, TagUniqueRename,
UniqueTest, Thing, TagThrough, BookWithM2MThrough, AuthorTag, AuthorWithM2MThrough) UniqueTest, Thing, TagThrough, BookWithM2MThrough, AuthorTag, AuthorWithM2MThrough)
...@@ -269,6 +270,25 @@ class SchemaTests(TransactionTestCase): ...@@ -269,6 +270,25 @@ class SchemaTests(TransactionTestCase):
# Make sure the values were transformed correctly # Make sure the values were transformed correctly
self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2) self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
def test_add_field_binary(self):
"""
Tests binary fields get a sane default (#22851)
"""
# Create the table
with connection.schema_editor() as editor:
editor.create_model(Author)
# Add the new field
new_field = BinaryField(blank=True)
new_field.set_attributes_from_name("bits")
with connection.schema_editor() as editor:
editor.add_field(
Author,
new_field,
)
# Ensure the field is right afterwards
columns = self.column_classes(Author)
self.assertEqual(columns['bits'][0], "BinaryField")
def test_alter(self): def test_alter(self):
""" """
Tests simple altering of fields Tests simple altering of fields
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment