Server Administration

Problems filtering django datetime field by month and day returns Null

Yesterday,we ran into an issue that was due to a mysql bug on how we retrieve a queryset based on day and month .Example

idinvoice Namefrom_datedue_date
1x_20210-06-0220210-06-0220210-07-02
2x_20210-06-0220210-06-0220210-07-02
3x_20210-06-0320210-06-0320210-07-03
Invoicer Table

A queryset to check if there are any open invoices for current month would be

import datetime
current_date=datetime.datetime.today()
Invoicer.objects.filter(is_open=True,from_date__month=current_date.month,from_date__year=current_date.year)

On the surface,this should return the 3 invoices,oddly an empty queryset is returned.

An empty queryset inadvertently means.No invoice exists hence generate a new one.

REASON

The error was due to MySQL configuration missing timezone data as suggested here

https://stackoverflow.com/questions/60810946/django-truncdate-gives-null/60844090#60844090

Correction

 mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot --force  mysql -p
mysql import timezone

Despite the skipping error the queryset returned the three records instead of None

Leave a Reply

Your email address will not be published. Required fields are marked *