__init__.py 808 B

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