Store aware times for better interaction with other Django apps

This commit is contained in:
Quantum 2024-04-12 21:34:15 -04:00
parent a6e057cf60
commit 944b96eea7
2 changed files with 20 additions and 1 deletions

19
peeringdb_api/backend.py Normal file
View file

@ -0,0 +1,19 @@
import zoneinfo
from django.db.models import DateTimeField, Model
from django.utils import timezone
from django_peeringdb import __version__ # noqa
from django_peeringdb.client_adaptor.backend import Backend as OldBackend
utc = zoneinfo.ZoneInfo('UTC')
class Backend(OldBackend):
def clean(self, obj: Model):
for field in obj._meta.get_fields():
if isinstance(field, DateTimeField):
value = getattr(obj, field.name)
if timezone.is_naive(value):
setattr(obj, field.name, timezone.make_aware(value, utc))
super().clean(obj)

View file

@ -1,4 +1,4 @@
from django_peeringdb.client_adaptor import backend
from peeringdb_api import backend
def load_backend(**kwargs):