migration.py 849 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django.db import models
  2. from timezone_field import TimeZoneField
  3. from netbox.config import ConfigItem
  4. __all__ = (
  5. 'custom_deconstruct',
  6. )
  7. SKIP_FIELDS = (
  8. TimeZoneField,
  9. )
  10. EXEMPT_ATTRS = (
  11. 'choices',
  12. 'help_text',
  13. 'verbose_name',
  14. )
  15. _deconstruct = models.Field.deconstruct
  16. def custom_deconstruct(field):
  17. """
  18. Imitate the behavior of the stock deconstruct() method, but ignore the field attributes listed above.
  19. """
  20. name, path, args, kwargs = _deconstruct(field)
  21. # Remove any ignored attributes
  22. if field.__class__ not in SKIP_FIELDS:
  23. for attr in EXEMPT_ATTRS:
  24. kwargs.pop(attr, None)
  25. # Ignore any field defaults which reference a ConfigItem
  26. kwargs = {
  27. k: v for k, v in kwargs.items() if not isinstance(v, ConfigItem)
  28. }
  29. return name, path, args, kwargs