#35491 closed New feature (invalid)
QuerySet.extra use case. Unnest array and make it lower. and make it an array again.
Reported by: | june | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.2 |
Severity: | Normal | Keywords: | QuerySet.extra |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I had used extra() for annotating Arrayfield with lower cases and filter out with another array intersection.
Below is what I used for
`
lower_value = [v.lower() for v in value]
numbers = Numeric.objects.extra(
where=[f"ARRAY(SELECT lower(unnest({array_field}))) && %s::text[]"],
params=[lower_value],
)
`
I need to unnest arrayfield first and make each elements lowercase, and make it an Array again.
and then need to check overlay with another array.
Note:
See TracTickets
for help on using tickets.
There are multiple ways to achieve that without the usage of
extra
.The most straightforward is to use
RawSQL
as mentioned in the docs right below the part that mentions using creating a ticket.But you could also use a
Func
It would also be pretty straightforward to define an
__loweroverlap
transform if you wanted to to encapsulate all of that logicIn short, you don't need
extra
at all to achieve what you're after. Please refer to the expression documentation and familiarize yourself withRawSQL
usage if you want to resort to raw sql usage.