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