0

Here's the structure of the JSONField - data I have.

data : {
 "animals" : [
     "animal_id" : 2321, 
     "legs" : [
      {
         "name" : "front",
         "measured_at" : 1596740795.453353
      },
      {
         "name" : "back",
         "measured_at": 1596740795.453353
      }
     ]
 ]
}

I need to find all records that match the following condition

  1. legs-name = "front" OR
  2. measured_at is greater than 6 hrs from now.

I have tried the following

six_hrs_ago = (datetime.utcnow() - timedelta(hours = 6)).timestamp()

obj = Model.objects.filter(data__has_key='animals').
        filter(Q(data__animals__legs__name="front") | Q(data__animals__legs__measured_at__gt = six_hrs_ago))))

This doesn't work, and it fetches no record.

I also tried to just filter the records with legs - name - front, that doesn't work either

obj = Model.objects.filter(data__animals__contains=[{'legs': [{"name": "front"}]}])

Any ideas are appreciated.

Tchinmai
  • 620
  • 1
  • 6
  • 20

0 Answers0