#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
--------------------------------+--------------------------------------
Comment (by Vasu Nagendra):

 Hi Simon -
 Thanks for the pointer. My apologies, I didn't realize `RawSQL` could be
 written like that by specifying an `output_field`. I thought the only way
 to do this was `extra` and the documentation suggested I file a ticket
 with my specific use case for consideration during deprecation. I will
 play with this a bit and then submit changes to docs as you suggested. I
 think generally your example there is perfect to add to the docs -- I can
 do that though, you don't need to. I think the right place where it
 belongs is here https://docs.djangoproject.com/en/5.0/topics/db/queries
 /#containment-and-key-lookups. This is the first place I looked to
 understand what is possible with `KT()` expressions. If you have a
 better/different place, that's OK too - please suggest.

 I'll test this out a bit with different use cases and add an example.

 In my specific use case I have a few keys/values like this that I am
 trying to mess around with for the `filter` clause. For everything that
 doesn't have a `*` I was easily able to get `Q` objects working with
 multiple `AND` and `OR` conditions and it beautifully does the right
 thing. I want to try out your example above where I'd prefer to write
 something like this (simplified, since I have other conditions that the
 PostgreSQL JSONPath operator doesn't support directly like `endswith`)...

 {{{
 conditions = []
 for k, v in search.items():
    if "*" in k:
       conditions.append(Q(**{f"data__{k}__anypath": v}))
    else:
       conditions.append(Q(**{f"data__{k)__exact": v}))
 for condition in conditions:
    combined &= Q(condition)
 Alert.objects.filter(combined)
 }}}

 I appreciate your patience and friendliness! Thank you!
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35568#comment:2>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070190662f2943-cae2375e-d59b-46a0-9e03-7258c0d950e4-000000%40eu-central-1.amazonses.com.

Reply via email to