#35568: Searching nested JSON values
--------------------------------+--------------------------------------
     Reporter:  Vasu Nagendra   |                    Owner:  nobody
         Type:  Uncategorized   |                   Status:  closed
    Component:  Uncategorized   |                  Version:  5.0
     Severity:  Normal          |               Resolution:  invalid
     Keywords:  QuerySet.extra  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------
Changes (by Simon Charette):

 * resolution:   => invalid
 * status:  new => closed

Comment:

 Hello Vasu,

 As referenced in the docs you can simply use `RawSQL` assuming you
 specifying an `output_field=BooleanField()`

 {{{#!python
 Alert.objects.filter(
     RawSQL("data @? %s", ('$.track.segments[*].HR ? (@ > 130)',),
 output_field=BooleanField())
 )
 }}}

 In the case of lookups though [https://docs.djangoproject.com/en/5.0/howto
 /custom-lookups/#a-lookup-example you can create your own] to avoid having
 to use `RawSQL` in the first place

 {{{#!python
 from django.db.models import JSONField, Lookup


 class AnyJSONPath(Lookup):
     lookup_name = "anypath"

     def as_postgresql(self, compiler, connection):
         lhs_sql, lhs_params = self.process_lhs(compiler, connection)
         rhs_sql, rhs_params = self.process_rhs(compiler, connection)
         params = lhs_params + rhs_params
         return f"{lhs_sql} @? {rhs_sql}", params

 JSONField.register_lookup(AnyJSONPath)
 }}}

 Are there any admonition that could have been made to the existing
 documentation that could have pointed you in the right direction without
 requiring the creation of ticket? Would you be interested in submitting
 them?
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35568#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070190661668db-f9341d92-911d-458a-b27b-89ddf777c6c6-000000%40eu-central-1.amazonses.com.

Reply via email to