Opened 6 years ago

Closed 3 years ago

#29168 closed Cleanup/optimization (invalid)

Document how to write a custom lookup where the rhs comes before the lhs

Reported by: JJ Gutierrez Owned by: nobody
Component: Documentation Version: 2.0
Severity: Normal Keywords: lookup
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The page for creating custom field lookups is incomplete. I am referring to the doc at https://docs.djangoproject.com/en/2.0/howto/custom-lookups/. The documentation is unclear about how to use the left-hand side value (lhs) and right-hand side value (rhs) properly. For example, lets say you have a lookup like

id__custom='abc123'

The lhs is the ID field and the rhs is the 'abc123'. I want to create a query like

WHERE 'abc123' LIKE id||%

However the documentation is unclear about how to reference the lhs in the as_sql method. Users would like to know how to reference the rhs BEFORE the LIKE and the lhs AFTER the LIKE in the sql query above. The documentation only shows examples where the lhs comes first and the rhs last. This would help users create a custom lookup with the sql above.

Change History (3)

comment:1 by Tim Graham, 6 years ago

Summary: Custom lookup documentation is incompleteDocument how to write a custom lookup where the rhs comes before the lhs
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Maybe looking at the built in lookups would offer a hint.

comment:2 by Tim Graham, 6 years ago

Component: Database layer (models, ORM)Documentation

comment:3 by Mariusz Felisiak, 3 years ago

Resolution: invalid
Status: newclosed
Triage Stage: AcceptedUnreviewed

However the documentation is unclear about how to reference the lhs in the as_sql method. Users would like to know how to reference the rhs BEFORE the LIKE and the lhs AFTER the LIKE in the sql query above. The documentation only shows examples where the lhs comes first and the rhs last. This would help users create a custom lookup with the sql above.

IMO A lookup example makes it really clear, I don't see how we could make it clearer. You need to swap lhs and rhs, e.g.:

class Custom(Lookup):
    lookup_name = 'custom'

    def as_sql(self, compiler, connection):
        lhs, lhs_params = self.process_lhs(compiler, connection)
        rhs, rhs_params = self.process_rhs(compiler, connection)
        params = rhs_params + lhs_params
        return '%s LIKE %s||%%' % (rhs, lhs), params

Closing as invalid.

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