Opened 15 years ago

Closed 15 years ago

#12517 closed (fixed)

Mistake in example for django.db.models.get_prep_lookup()

Reported by: django@… Owned by: nobody
Component: Documentation Version: dev
Severity: Keywords:
Cc: 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

http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.get_prep_lookup

In the example, the return value for an exact lookup is wrapped in a list:

    def get_prep_lookup(self, lookup_type, value):
        # We only handle 'exact' and 'in'. All others are errors.
        if lookup_type == 'exact':
            return [self.get_prep_value(value)]

This then gets passed to the DB connection's compiler in get_db_prep_lookup, and seeing a list, does a typecast to ARRAY (in the case of Postgres).

I think the example should in fact return just the value, not wrapped in a list (also as per the default implementation of get_prep_lookup in the source):

    def get_prep_lookup(self, lookup_type, value):
        # We only handle 'exact' and 'in'. All others are errors.
        if lookup_type == 'exact':
            return self.get_prep_value(value)

By making this change to a custom field I have, the undesired typecast behaviour described above is avoided.

Attachments (1)

12517.diff (633 bytes ) - added by Tim Graham 15 years ago.

Download all attachments as: .zip

Change History (3)

by Tim Graham, 15 years ago

Attachment: 12517.diff added

comment:1 by Tim Graham, 15 years ago

Has patch: set
Triage Stage: UnreviewedReady for checkin

comment:2 by Russell Keith-Magee, 15 years ago

Resolution: fixed
Status: newclosed

(In [13213]) Fixed #12517 -- Corrected get_prep_lookup example in custom field docs. Thanks to django@… for the report.

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