From 7ec415584575acb5968c1e3e038c67bbc8eec029 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 15 Aug 2018 14:18:52 -0400 Subject: [PATCH] Closes #2359: Implement custom makemigrations command to ignore extraneous field attributes --- netbox/utilities/management/__init__.py | 0 .../utilities/management/commands/__init__.py | 0 .../management/commands/makemigrations.py | 32 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 netbox/utilities/management/__init__.py create mode 100644 netbox/utilities/management/commands/__init__.py create mode 100644 netbox/utilities/management/commands/makemigrations.py diff --git a/netbox/utilities/management/__init__.py b/netbox/utilities/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/utilities/management/commands/__init__.py b/netbox/utilities/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/utilities/management/commands/makemigrations.py b/netbox/utilities/management/commands/makemigrations.py new file mode 100644 index 000000000..7ebf19748 --- /dev/null +++ b/netbox/utilities/management/commands/makemigrations.py @@ -0,0 +1,32 @@ +from django.core.management.commands.makemigrations import Command +from django.db import models + + +EXEMPT_ATTRS = [ + 'choices', + 'help_text', + 'verbose_name', +] + +_deconstruct = models.Field.deconstruct + + +def custom_deconstruct(field): + """ + Imitate the behavior of the stock deconstruct() method, but ignore the field attributes listed above. + """ + name, path, args, kwargs = _deconstruct(field) + + # Remove any ignored attributes + for attr in EXEMPT_ATTRS: + kwargs.pop(attr, None) + + # A hack to accommodate TimeZoneField, which employs a custom deconstructor to check whether the default choices + # have changed + if hasattr(field, 'CHOICES'): + kwargs['choices'] = field.CHOICES + + return name, path, args, kwargs + + +models.Field.deconstruct = custom_deconstruct