Opened 6 years ago

Closed 5 years ago

#12517 closed (fixed)

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

Reported by: django@… Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 timo 6 years ago.

Download all attachments as: .zip

Change History (3)

Changed 6 years ago by timo

comment:1 Changed 6 years ago by timo

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

comment:2 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(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