#35344: GeneratedField get_col output_field bug
-------------------------------------+-------------------------------------
Reporter: Johannes Westphal | Owner: Johannes
| Westphal
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Johannes Westphal):
Here a minimal example to reproduce the crash:
**Model**
{{{#!python
from django.contrib.gis.db import models
from django.contrib.postgres.fields import DateTimeRangeField
from django.contrib.postgres.functions import TransactionNow
class DateTimeRange(models.Func):
function = 'tstzrange'
template = "%(function)s(%(expressions)s)"
output_field = DateTimeRangeField()
def __init__(self, a, b):
super().__init__(a, b)
class P(models.Model):
start = models.DateTimeField(db_default=TransactionNow())
finish = models.DateTimeField(null=True)
timerange =
models.GeneratedField(expression=DateTimeRange(models.F('start'),
models.F('finish')), output_field=DateTimeRangeField(), db_persist=True)
class T(models.Model):
p1 = models.ForeignKey(P, on_delete=models.CASCADE,
related_name='tp1s')
p2 = models.ForeignKey(P, on_delete=models.CASCADE,
related_name='tp2s')
}}}
**Query**
{{{#!python
from django.utils import timezone
now = timezone.now()
T.objects.filter(
p1__timerange__contains=now,
p2__timerange__contains=now,
).count()
}}}
**Traceback**
{{{
Traceback (most recent call last):
File "test.py", line 14, in <module>
).count()
File "django/db/models/query.py", line 620, in count
return self.query.get_count(using=self.db)
File "django/db/models/sql/query.py", line 629, in get_count
return obj.get_aggregation(using, {"__count": Count("*")})["__count"]
File "django/db/models/sql/query.py", line 615, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "django/db/models/sql/compiler.py", line 1549, in execute_sql
sql, params = self.as_sql()
File "django/db/models/sql/compiler.py", line 764, in as_sql
self.compile(self.where) if self.where is not None else ("", [])
File "django/db/models/sql/compiler.py", line 546, in compile
sql, params = node.as_sql(self, self.connection)
File "django/db/models/sql/where.py", line 151, in as_sql
sql, params = compiler.compile(child)
File "django/db/models/sql/compiler.py", line 544, in compile
sql, params = vendor_impl(self, self.connection)
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'
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/35344#comment:9>