From 8fc605037af858133dfcbdb39d7b5227915646ba Mon Sep 17 00:00:00 2001 From: Sergio Saucedo Date: Tue, 8 Feb 2022 01:26:26 -0600 Subject: [PATCH] Implement custom DateColumn improving null values handling --- netbox/circuits/tables.py | 3 --- netbox/utilities/tables.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/netbox/circuits/tables.py b/netbox/circuits/tables.py index ef34fb908..889792be3 100644 --- a/netbox/circuits/tables.py +++ b/netbox/circuits/tables.py @@ -140,9 +140,6 @@ class CircuitTable(BaseTable): template_code=CIRCUITTERMINATION_LINK, verbose_name='Side Z' ) - install_date = tables.DateColumn( - default='' - ) commit_rate = CommitRateColumn() comments = MarkdownColumn() tags = TagColumn( diff --git a/netbox/utilities/tables.py b/netbox/utilities/tables.py index c640e0e85..8a54b82d9 100644 --- a/netbox/utilities/tables.py +++ b/netbox/utilities/tables.py @@ -4,10 +4,12 @@ from django.contrib.auth.models import AnonymousUser from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.core.exceptions import FieldDoesNotExist +from django.db.models import DateField from django.db.models.fields.related import RelatedField from django.urls import reverse from django.utils.safestring import mark_safe from django_tables2 import RequestConfig +from django_tables2.columns import library from django_tables2.data import TableQuerysetData from django_tables2.utils import Accessor @@ -205,6 +207,23 @@ class TemplateColumn(tables.TemplateColumn): return ret +@library.register +class DateColumn(tables.DateColumn): + """ + Overrides the default implementation of DateColumn to better handle null values, returning a default value for + tables and null when exporting data. It is registered in the tables library to use this class instead of the + default, making this behavior consistent in all fields of type DateField. + """ + + def value(self, value): + return value + + @classmethod + def from_field(cls, field, **kwargs): + if isinstance(field, DateField): + return cls(**kwargs) + + class ButtonsColumn(tables.TemplateColumn): """ Render edit, delete, and changelog buttons for an object.