Opened 7 years ago

Closed 12 months ago

Last modified 5 months ago

#29049 closed New feature (fixed)

Add slicing notation to F expressions

Reported by: Matthew Pava Owned by: Nick Pope
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: slice F
Cc: Adam Johnson, josh.smeaton@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For fields that are string expressions, the slicing notation should utilize database functions Substr, Left, and Right. Those functions could even be deprecated to require the use of slicing notation.

For fields that are iterable expressions, such as ArrayFields or HStoreFields, those could utilize slicing notation in a Pythonic manner.

Change History (28)

comment:1 by Tim Graham, 7 years ago

Could you give an example of what the syntax would look like?

comment:2 by Simon Charette, 7 years ago

I think this is a reference to my post on the developers mailing list.

That would be F('textfield')[0:5] which would map to SubStr and F('arrayfield')[0:10] which would map to PostgreSQL arrayfield[1:10].

comment:3 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Adam Johnson, 7 years ago

Cc: Adam Johnson added

comment:5 by Priyansh Saxena, 7 years ago

Needs documentation: set
Needs tests: set
Owner: changed from nobody to Priyansh Saxena
Status: newassigned

comment:6 by Josh Smeaton, 7 years ago

Cc: josh.smeaton@… added

comment:7 by Priyansh Saxena, 7 years ago

Has patch: set

comment:8 by Tim Graham, 7 years ago

Needs documentation: unset
Needs tests: unset

comment:9 by Carlton Gibson, 7 years ago

Patch needs improvement: set

Comments on PR.

comment:10 by Niclas Olofsson, 6 years ago

Owner: changed from Priyansh Saxena to Niclas Olofsson

Taking over this since the current asignee hasn't worked on it in a while.

comment:11 by Niclas Olofsson, 6 years ago

Patch needs improvement: unset

comment:12 by Mariusz Felisiak, 5 years ago

Needs tests: set
Patch needs improvement: set

comment:13 by Sergey Fedoseev, 4 years ago

Cc: Sergey Fedoseev added

comment:14 by Niclas Olofsson, 4 years ago

Owner: Niclas Olofsson removed
Status: assignednew

Since feedback on the review came 10 months after I submitted the patch, and it now has been more than a year since it was submitted, it would be too much of an effort for me to put my mind into this again and get it into a mergable state.

comment:15 by Sergey Fedoseev, 4 years ago

Owner: set to Sergey Fedoseev
Status: newassigned

comment:16 by David Smith, 3 years ago

Owner: changed from Sergey Fedoseev to David Smith

comment:17 by Sergey Fedoseev, 3 years ago

Cc: Sergey Fedoseev removed

comment:18 by David Smith, 3 years ago

Needs tests: unset
Patch needs improvement: unset
Last edited 3 years ago by Mariusz Felisiak (previous) (diff)

comment:19 by Mariusz Felisiak, 3 years ago

Needs tests: set
Patch needs improvement: set

comment:20 by Mariusz Felisiak, 3 years ago

Needs tests: unset

comment:21 by Abhinav Yadav, 2 years ago

Owner: changed from David Smith to Abhinav Yadav

comment:22 by Abhinav Yadav, 2 years ago

I have been reading up about this issue to continue and finish it off from where David left it, and I have some doubts. The SQL query left by Felix in this comment also doesn't generate any QuerySet or objects. I tried to run that query using Manager.raw(), which generated no results.

Further I investigated the resolve_expression method of SliceableF class in django/db/models/expressions.py, and compared it to other implementations of resolve_expression method in other classes but couldn't find anything concrete which might point to where the error might be.

Does a special method for handling slicing and indexing for OuterRef query need to be written so it is properly resolved? That is what I'm thinking because I can't find anything else that is properly handling slicing for OuterRef Queries, unless I'm missing something.

Also should None be used instead of '' in slice_expression for end if length is not provided?

Last edited 2 years ago by Abhinav Yadav (previous) (diff)

comment:23 by Abhinav Yadav, 2 years ago

Patch needs improvement: unset

comment:24 by Mariusz Felisiak, 2 years ago

Patch needs improvement: set

comment:25 by Nick Pope, 12 months ago

Owner: changed from Abhinav Yadav to Nick Pope
Patch needs improvement: unset

New PR with some fixes. Hopefully we can finally get this over the line...

comment:26 by Mariusz Felisiak, 12 months ago

Triage Stage: AcceptedReady for checkin

comment:27 by GitHub <noreply@…>, 12 months ago

Resolution: fixed
Status: assignedclosed

In 94b6f10:

Fixed #29049 -- Added slicing notation to F expressions.

Co-authored-by: Priyansh Saxena <askpriyansh@…>
Co-authored-by: Niclas Olofsson <n@…>
Co-authored-by: David Smith <smithdc@…>
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@…>
Co-authored-by: Abhinav Yadav <abhinav.sny.2002@…>

comment:28 by GitHub <noreply@…>, 5 months ago

In 9f5e230:

Refs #29049 -- Fixed isolation of BasicExpressionsTests._test_slicing_of_f_expressions() subtests.

Thanks Tim Graham for the report.

Note: See TracTickets for help on using tickets.
Back to Top