From 621c25c419ae0f4d6d0f76725e5f3585d76228d0 Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Fri, 6 Dec 2013 15:21:13 +0100 Subject: [PATCH] Added missing deconstruct() methods. --- django/db/models/__init__.py | 1 + django/db/models/fields/__init__.py | 2 ++ django/db/models/fields/proxy.py | 5 +++++ django/db/models/fields/related.py | 16 ++++++++++++---- tests/serializers/models.py | 5 +++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/django/db/models/__init__.py b/django/db/models/__init__.py index e2f7348ef0..27db994b61 100644 --- a/django/db/models/__init__.py +++ b/django/db/models/__init__.py @@ -15,6 +15,7 @@ from django.db.models.fields.files import FileField, ImageField # NOQA from django.db.models.fields.related import ( # NOQA ForeignKey, ForeignObject, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel) +from django.db.models.fields.proxy import OrderWrt from django.db.models.deletion import ( # NOQA CASCADE, PROTECT, SET, SET_NULL, SET_DEFAULT, DO_NOTHING, ProtectedError) from django.db.models import signals # NOQA diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 6b2dc2ad21..826471d9ed 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -230,6 +230,8 @@ class Field(object): path = path.replace("django.db.models.fields.related", "django.db.models") if path.startswith("django.db.models.fields.files"): path = path.replace("django.db.models.fields.files", "django.db.models") + if path.startswith("django.db.models.fields.proxy"): + path = path.replace("django.db.models.fields.proxy", "django.db.models") if path.startswith("django.db.models.fields"): path = path.replace("django.db.models.fields", "django.db.models") # Return basic info - other fields should override this. diff --git a/django/db/models/fields/proxy.py b/django/db/models/fields/proxy.py index 29c782b59a..19beb89011 100644 --- a/django/db/models/fields/proxy.py +++ b/django/db/models/fields/proxy.py @@ -16,3 +16,8 @@ class OrderWrt(fields.IntegerField): kwargs['name'] = '_order' kwargs['editable'] = False super(OrderWrt, self).__init__(*args, **kwargs) + + def deconstruct(self): + name, path, args, kwargs = super(OrderWrt, self).deconstruct() + del kwargs['editable'] + return name, path, args, kwargs diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 23b508ebb5..be796f2af1 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -1019,6 +1019,16 @@ class ForeignObject(RelatedField): super(ForeignObject, self).__init__(**kwargs) + def deconstruct(self): + name, path, args, kwargs = super(ForeignObject, self).deconstruct() + kwargs['from_fields'] = self.from_fields + kwargs['to_fields'] = self.to_fields + if isinstance(self.rel.to, six.string_types): + kwargs['to'] = self.rel.to + else: + kwargs['to'] = "%s.%s" % (self.rel.to._meta.app_label, self.rel.to._meta.object_name) + return name, path, args, kwargs + def resolve_related_fields(self): if len(self.from_fields) < 1 or len(self.from_fields) != len(self.to_fields): raise ValueError('Foreign Object from and to fields must be the same non-zero length') @@ -1243,6 +1253,8 @@ class ForeignKey(ForeignObject): def deconstruct(self): name, path, args, kwargs = super(ForeignKey, self).deconstruct() + del kwargs['to_fields'] + del kwargs['from_fields'] # Handle the simpler arguments if self.db_index: del kwargs['db_index'] @@ -1255,10 +1267,6 @@ class ForeignKey(ForeignObject): # Rel needs more work. if self.rel.field_name: kwargs['to_field'] = self.rel.field_name - if isinstance(self.rel.to, six.string_types): - kwargs['to'] = self.rel.to - else: - kwargs['to'] = "%s.%s" % (self.rel.to._meta.app_label, self.rel.to._meta.object_name) return name, path, args, kwargs @property diff --git a/tests/serializers/models.py b/tests/serializers/models.py index 680d463ad0..691d87ea29 100644 --- a/tests/serializers/models.py +++ b/tests/serializers/models.py @@ -115,6 +115,11 @@ class TeamField(six.with_metaclass(models.SubfieldBase, models.CharField)): def value_to_string(self, obj): return self._get_val_from_obj(obj).to_string() + def deconstruct(self): + name, path, args, kwargs = super(TeamField, self).deconstruct() + del kwargs['max_length'] + return name, path, args, kwargs + @python_2_unicode_compatible class Player(models.Model):