Skip to content

Commit 79190e2

Browse files
committed
[fix]: Prevent FallbackMixin from generating spurious migrations
The deconstruct() method was serializing the fallback kwarg into Django migration files. This caused new migrations to be generated whenever the fallback default value changed in settings, even though no actual database schema change had occurred. The fix removes fallback from deconstruct() so Django no longer tracks it as part of the field migration state. fallback is also made optional in __init__ (defaulting to None) so existing migrations that omit the kwarg remain valid. Fixes: #1231
1 parent cc2e830 commit 79190e2

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

openwisp_utils/fields.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ class FallbackMixin(object):
4747
field will save `None` in the database.
4848
"""
4949

50+
non_db_attrs = ("fallback",)
51+
5052
def __init__(self, *args, **kwargs):
51-
self.fallback = kwargs.pop("fallback")
53+
self.fallback = kwargs.pop("fallback", None)
5254
opts = dict(blank=True, null=True, default=None)
5355
opts.update(kwargs)
5456
super().__init__(*args, **opts)
5557

5658
def deconstruct(self):
5759
name, path, args, kwargs = super().deconstruct()
58-
kwargs["fallback"] = self.fallback
60+
if self.fallback is not None:
61+
kwargs["fallback"] = self.fallback
5962
return (name, path, args, kwargs)
6063

6164
def from_db_value(self, value, expression, connection):

tests/test_project/tests/test_model.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,17 @@ def test_fallback_decimal_field(self):
180180
book.save(update_fields=["price"])
181181
book.refresh_from_db(fields=["price"])
182182
self.assertEqual(book.price, 56)
183+
184+
def test_fallback_field_deconstruct(self):
185+
field = OrganizationRadiusSettings._meta.get_field("is_active")
186+
name, path, args, kwargs = field.deconstruct()
187+
self.assertIn("fallback", kwargs)
188+
field = OrganizationRadiusSettings._meta.get_field("greeting_text")
189+
name, path, args, kwargs = field.deconstruct()
190+
self.assertIn("fallback", kwargs)
191+
field = Book._meta.get_field("price")
192+
name, path, args, kwargs = field.deconstruct()
193+
self.assertIn("fallback", kwargs)
194+
field = Shelf._meta.get_field("books_count")
195+
name, path, args, kwargs = field.deconstruct()
196+
self.assertIn("fallback", kwargs)

0 commit comments

Comments
 (0)