I have a model in Django where there is a JsonField named periodicity which serialize in an Json Array like this:
{
"id_series": 12381,
"country_name": "Central African Republic",
"periodicity": [
{
"period": "monthly",
"start_date": "2010-10-01",
"end_date": "2018-10-01"
},
{
"period": "weekly",
"start_date": "2011-10-01",
"end_date": "2018-01-01"
}
]
},
{
...
}
So my aim is to filter and find the series that have not been updated in less than a certain date. For that I need to query on the end_date of the inner Json Array. What is the best way to do a filtering?
I have tried casting the end_date to a date object by doing this :
from django.db.models.expressions import RawSQL
Serie.objects.annotate(endDate=RawSQL("((periodicity->>'end_date')::date)", [])).filter(endDate__gte=comparisonDate)
But so far it has not produced any result. I am looking into something like this https://stackoverflow.com/a/65184218/2219080.