﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35344	GeneratedField get_col output_field bug	Johannes Westphal	Johannes Westphal	"Related issue #34838.

For generated fields, `get_col` gets called with `output_field=<instance of the generated field>`. Consequently, if an `alias` is specified, the `output_field` of the generated `Col` is of type `GeneratedField` instead of the actual `output_field` of the `GeneratedField`, since the current code only handles the case where `get_col`'s `output_field` parameter is `None`, but not when it is `self`.

**Error**
{{{
File ""django/contrib/postgres/fields/ranges.py"", line 253, in as_postgresql
    cast_internal_type = self.lhs.output_field.base_field.get_internal_type()
AttributeError: 'GeneratedField' object has no attribute 'base_field'
}}}

**Patch**

{{{#!diff
diff --git a/django/db/models/fields/generated.py b/django/db/models/fields/generated.py
index 257feeeba2..5b6b188df0 100644
--- a/django/db/models/fields/generated.py
+++ b/django/db/models/fields/generated.py
@@ -39,7 +39,7 @@ class GeneratedField(Field):
         return Col(self.model._meta.db_table, self, self.output_field)
 
     def get_col(self, alias, output_field=None):
-        if alias != self.model._meta.db_table and output_field is None:
+        if alias != self.model._meta.db_table and output_field in (None, self):
             output_field = self.output_field
         return super().get_col(alias, output_field)
}}}

"	Bug	closed	Database layer (models, ORM)	dev	Release blocker	fixed		Johannes Westphal	Ready for checkin	1	0	0	0	0	0
