Skip to content

Commit c0a6279

Browse files
Merge pull request #8441 from seulsale/8391-install-date-null
Fixes #8391: Install date should appear empty when exported
2 parents dd848d7 + f80452c commit c0a6279

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

netbox/utilities/tables.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
from django.contrib.contenttypes.fields import GenericForeignKey
55
from django.contrib.contenttypes.models import ContentType
66
from django.core.exceptions import FieldDoesNotExist
7+
from django.db.models import DateField, DateTimeField
78
from django.db.models.fields.related import RelatedField
89
from django.urls import reverse
10+
from django.utils.formats import date_format
911
from django.utils.safestring import mark_safe
1012
from django_tables2 import RequestConfig
13+
from django_tables2.columns import library
1114
from django_tables2.data import TableQuerysetData
1215
from django_tables2.utils import Accessor
1316

@@ -205,6 +208,42 @@ def value(self, **kwargs):
205208
return ret
206209

207210

211+
@library.register
212+
class DateColumn(tables.DateColumn):
213+
"""
214+
Overrides the default implementation of DateColumn to better handle null values, returning a default value for
215+
tables and null when exporting data. It is registered in the tables library to use this class instead of the
216+
default, making this behavior consistent in all fields of type DateField.
217+
"""
218+
219+
def value(self, value):
220+
return value
221+
222+
@classmethod
223+
def from_field(cls, field, **kwargs):
224+
if isinstance(field, DateField):
225+
return cls(**kwargs)
226+
227+
228+
@library.register
229+
class DateTimeColumn(tables.DateTimeColumn):
230+
"""
231+
Overrides the default implementation of DateTimeColumn to better handle null values, returning a default value for
232+
tables and null when exporting data. It is registered in the tables library to use this class instead of the
233+
default, making this behavior consistent in all fields of type DateTimeField.
234+
"""
235+
236+
def value(self, value):
237+
if value:
238+
return date_format(value, format="SHORT_DATETIME_FORMAT")
239+
return None
240+
241+
@classmethod
242+
def from_field(cls, field, **kwargs):
243+
if isinstance(field, DateTimeField):
244+
return cls(**kwargs)
245+
246+
208247
class ButtonsColumn(tables.TemplateColumn):
209248
"""
210249
Render edit, delete, and changelog buttons for an object.

0 commit comments

Comments
 (0)