Kaydet (Commit) 912ad032 authored tarafından Markus Holtermann's avatar Markus Holtermann Kaydeden (comit) Tim Graham

Fixed #23894 -- Made deconstruct methods favor kwargs over args

üst e023ceb4
......@@ -83,8 +83,10 @@ class ArrayField(Field):
def deconstruct(self):
name, path, args, kwargs = super(ArrayField, self).deconstruct()
path = 'django.contrib.postgres.fields.ArrayField'
args.insert(0, self.base_field)
kwargs['size'] = self.size
kwargs.update({
'base_field': self.base_field,
'size': self.size,
})
return name, path, args, kwargs
def to_python(self, value):
......
......@@ -17,12 +17,16 @@ class AddField(Operation):
self.preserve_default = preserve_default
def deconstruct(self):
kwargs = {}
kwargs = {
'model_name': self.model_name,
'name': self.name,
'field': self.field,
}
if self.preserve_default is not True:
kwargs['preserve_default'] = self.preserve_default
return (
self.__class__.__name__,
[self.model_name, self.name, self.field],
[],
kwargs
)
......@@ -74,10 +78,14 @@ class RemoveField(Operation):
self.name = name
def deconstruct(self):
kwargs = {
'model_name': self.model_name,
'name': self.name,
}
return (
self.__class__.__name__,
[self.model_name, self.name],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......@@ -120,12 +128,16 @@ class AlterField(Operation):
self.preserve_default = preserve_default
def deconstruct(self):
kwargs = {}
kwargs = {
'model_name': self.model_name,
'name': self.name,
'field': self.field,
}
if self.preserve_default is not True:
kwargs['preserve_default'] = self.preserve_default
return (
self.__class__.__name__,
[self.model_name, self.name, self.field],
[],
kwargs
)
......@@ -183,10 +195,15 @@ class RenameField(Operation):
self.new_name = new_name
def deconstruct(self):
kwargs = {
'model_name': self.model_name,
'old_name': self.old_name,
'new_name': self.new_name,
}
return (
self.__class__.__name__,
[self.model_name, self.old_name, self.new_name],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......
......@@ -21,14 +21,17 @@ class CreateModel(Operation):
self.bases = bases or (models.Model,)
def deconstruct(self):
kwargs = {}
kwargs = {
'name': self.name,
'fields': self.fields,
}
if self.options:
kwargs['options'] = self.options
if self.bases and self.bases != (models.Model,):
kwargs['bases'] = self.bases
return (
self.__class__.__name__,
[self.name, self.fields],
[],
kwargs
)
......@@ -83,10 +86,13 @@ class DeleteModel(Operation):
self.name = name
def deconstruct(self):
kwargs = {
'name': self.name,
}
return (
self.__class__.__name__,
[self.name],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......@@ -121,10 +127,14 @@ class RenameModel(Operation):
self.new_name = new_name
def deconstruct(self):
kwargs = {
'old_name': self.old_name,
'new_name': self.new_name,
}
return (
self.__class__.__name__,
[self.old_name, self.new_name],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......@@ -214,10 +224,14 @@ class AlterModelTable(Operation):
self.table = table
def deconstruct(self):
kwargs = {
'name': self.name,
'table': self.table,
}
return (
self.__class__.__name__,
[self.name, self.table],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......@@ -266,10 +280,14 @@ class AlterUniqueTogether(Operation):
self.unique_together = set(tuple(cons) for cons in unique_together)
def deconstruct(self):
kwargs = {
'name': self.name,
'unique_together': self.unique_together,
}
return (
self.__class__.__name__,
[self.name, self.unique_together],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......@@ -311,10 +329,14 @@ class AlterIndexTogether(Operation):
self.index_together = set(tuple(cons) for cons in index_together)
def deconstruct(self):
kwargs = {
'name': self.name,
'index_together': self.index_together,
}
return (
self.__class__.__name__,
[self.name, self.index_together],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......@@ -353,10 +375,14 @@ class AlterOrderWithRespectTo(Operation):
self.order_with_respect_to = order_with_respect_to
def deconstruct(self):
kwargs = {
'name': self.name,
'order_with_respect_to': self.order_with_respect_to,
}
return (
self.__class__.__name__,
[self.name, self.order_with_respect_to],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......@@ -412,10 +438,14 @@ class AlterModelOptions(Operation):
self.options = options
def deconstruct(self):
kwargs = {
'name': self.name,
'options': self.options,
}
return (
self.__class__.__name__,
[self.name, self.options],
{}
[],
kwargs
)
def state_forwards(self, app_label, state):
......
......@@ -68,14 +68,16 @@ class RunSQL(Operation):
self.state_operations = state_operations or []
def deconstruct(self):
kwargs = {}
kwargs = {
'sql': self.sql,
}
if self.reverse_sql is not None:
kwargs['reverse_sql'] = self.reverse_sql
if self.state_operations:
kwargs['state_operations'] = self.state_operations
return (
self.__class__.__name__,
[self.sql],
[],
kwargs
)
......@@ -137,14 +139,16 @@ class RunPython(Operation):
self.reverse_code = reverse_code
def deconstruct(self):
kwargs = {}
if self.reverse_code:
kwargs = {
'code': self.code,
}
if self.reverse_code is not None:
kwargs['reverse_code'] = self.reverse_code
if self.atomic is not True:
kwargs['atomic'] = self.atomic
return (
self.__class__.__name__,
[self.code],
[],
kwargs
)
......
......@@ -6,7 +6,6 @@ from django.contrib.postgres.forms import SimpleArrayField, SplitArrayField
from django.core import exceptions, serializers
from django.core.management import call_command
from django.db import models, IntegrityError, connection
from django.db.migrations.writer import MigrationWriter
from django import forms
from django.test import TestCase, override_settings
from django.utils import timezone
......@@ -229,11 +228,6 @@ class TestMigrations(TestCase):
new = ArrayField(*args, **kwargs)
self.assertEqual(new.base_field.max_length, field.base_field.max_length)
def test_makemigrations(self):
field = ArrayField(models.CharField(max_length=20))
statement, imports = MigrationWriter.serialize(field)
self.assertEqual(statement, 'django.contrib.postgres.fields.ArrayField(models.CharField(max_length=20), size=None)')
@override_settings(MIGRATION_MODULES={
"postgres_tests": "postgres_tests.array_default_migrations",
})
......
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