[1.7.x] Fixed #22851: BinaryView wasn't getting a binary default
This commit is contained in:
parent
dc7d0f50e2
commit
6e7da2bfaf
|
@ -8,7 +8,7 @@ from django.db.transaction import atomic
|
|||
from django.utils.encoding import force_bytes
|
||||
from django.utils.log import getLogger
|
||||
from django.utils.six.moves import reduce
|
||||
from django.utils.six import callable
|
||||
from django.utils import six
|
||||
|
||||
logger = getLogger('django.db.backends.schema')
|
||||
|
||||
|
@ -169,11 +169,14 @@ class BaseDatabaseSchemaEditor(object):
|
|||
if field.has_default():
|
||||
default = field.get_default()
|
||||
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:
|
||||
default = None
|
||||
# If it's a callable, call it
|
||||
if callable(default):
|
||||
if six.callable(default):
|
||||
default = default()
|
||||
# Run it through the field's get_db_prep_save method so we can send it
|
||||
# to the database.
|
||||
|
|
|
@ -4,9 +4,10 @@ import unittest
|
|||
|
||||
from django.test import TransactionTestCase
|
||||
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.transaction import atomic
|
||||
from django.utils import six
|
||||
from .models import (Author, AuthorWithM2M, Book, BookWithLongName,
|
||||
BookWithSlug, BookWithM2M, Tag, TagIndexed, TagM2MTest, TagUniqueRename,
|
||||
UniqueTest, Thing, TagThrough, BookWithM2MThrough, AuthorTag, AuthorWithM2MThrough)
|
||||
|
@ -269,6 +270,25 @@ class SchemaTests(TransactionTestCase):
|
|||
# Make sure the values were transformed correctly
|
||||
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):
|
||||
"""
|
||||
Tests simple altering of fields
|
||||
|
|
Loading…
Reference in New Issue