#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 , 8 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Owner: | changed from | to
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
Version: | 1.10 → master |
comment:2 by , 8 years ago
comment:3 by , 8 years ago
Has patch: | set |
---|
Note:
See TracTickets
for help on using tickets.
Here's a PR with the documentation adjustments.