Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27209 closed Cleanup/optimization (fixed)

Cast function accepts field class according to docs but expects field instance

Reported by: Valentin Ignatyev Owned by: Simon Charette
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Example from docs about django.db.models.functions.Cast will fail with TypeError: db_type() missing 1 required positional argument: 'connection' because in as_sql method when adding db_type to extra_context we execute db_type method of field class and not field instance:

 if 'db_type' not in extra_context:
    extra_context['db_type'] = self._output_field.db_type(connection)

Here self._output_field is a class instead of instance. So we either must pass an instance of field like

value = Value.objects.annotate(as_float=Cast('integer', FloatField())).get()

and point to it in docs or create instance inside as_sql like this:

extra_context['db_type'] = self._output_field().db_type(connection)

Change History (5)

comment:1 by Simon Charette, 8 years ago

Component: Database layer (models, ORM)Documentation
Owner: changed from nobody to Simon Charette
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization
Version: 1.10master

comment:2 by Simon Charette, 8 years ago

Here's a PR with the documentation adjustments.

comment:3 by Simon Charette, 8 years ago

Has patch: set

comment:4 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 767ba009:

Fixed #27209 -- Fixed typo in docs/ref/models/database-functions.txt.

Thanks Valentin Ignatyev for the report.

comment:5 by Tim Graham <timograham@…>, 8 years ago

In 23cf7e5:

[1.10.x] Fixed #27209 -- Fixed typo in docs/ref/models/database-functions.txt.

Thanks Valentin Ignatyev for the report.

Backport of 767ba009764c6640b20ea6a6dc6ee2559cc87800 from master

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