Commit a979f2ef by Daniel Hahler

Handle non-default kwargs in deconstruct

According to the documentation [1], non-default kwargs need to get
returned.

1: https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#field-deconstruction
parent 58d73e36
...@@ -92,9 +92,16 @@ class TaggableManager(RelatedField, Field): ...@@ -92,9 +92,16 @@ class TaggableManager(RelatedField, Field):
return manager return manager
def deconstruct(self): def deconstruct(self):
"""
Deconstruct the object, used with migrations.
"""
name, path, args, kwargs = super(TaggableManager, self).deconstruct() name, path, args, kwargs = super(TaggableManager, self).deconstruct()
# Remove forced kwargs.
for kwarg in ['serialize', 'null']: for kwarg in ['serialize', 'null']:
del kwargs[kwarg] del kwargs[kwarg]
# Add non-default arguments.
if self.through is not TaggedItem:
kwargs['through'] = self.through
return name, path, args, kwargs return name, path, args, kwargs
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
......
...@@ -538,3 +538,11 @@ class TagStringParseTestCase(UnitTestCase): ...@@ -538,3 +538,11 @@ class TagStringParseTestCase(UnitTestCase):
self.assertEqual(edit_string_for_tags([plain, spaces, comma]), '"com,ma", "spa ces", plain') self.assertEqual(edit_string_for_tags([plain, spaces, comma]), '"com,ma", "spa ces", plain')
self.assertEqual(edit_string_for_tags([plain, comma]), '"com,ma", plain') self.assertEqual(edit_string_for_tags([plain, comma]), '"com,ma", plain')
self.assertEqual(edit_string_for_tags([comma, spaces]), '"com,ma", "spa ces"') self.assertEqual(edit_string_for_tags([comma, spaces]), '"com,ma", "spa ces"')
class DeconstructTestCase(UnitTestCase):
def test_deconstruct_kwargs_kept(self):
instance = TaggableManager(through=OfficialThroughModel)
name, path, args, kwargs = instance.deconstruct()
new_instance = TaggableManager(*args, **kwargs)
self.assertEqual(instance.rel.through, new_instance.rel.through)
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 sign in to comment