'Returning a Unix Timestamp when querying a DateTimeField

In Django, I have the following query

dates = (
  Event.objects.filter(cancelled=False)
  .exclude(id=event.id)
  .aggregate(
    first_date=Min('start'),
    last_date=Max('start'),
    next_date=Min('start', filter=Q(date__gt=timezone.now().date())),
    recent_date=Max('start', filter=Q(end__lt=timezone.now()))
  )
)

and I would like to annotate the query so that my lesson dates are returned as unix timestamps rather than datetime objects. I recognize I can simply do something like dates.next_date.timestamp(), but this causes an error when dates.next_date is None, and I'd rather not have to repeatedly check that my object is not None before getting its timestamp. How could I go about this?

I have searched through many other answers and the Django docs but haven't been able to find anything I could specifically apply here. Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source