Opened 3 years ago
Closed 3 years ago
#32750 closed Bug (fixed)
ExtractMonth from OuterRef in Subquery don't work
Reported by: | Chiorufarewerin | Owned by: | Chiorufarewerin |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Just example code:
DTModel.objects.create(name='1', start_datetime=datetime(2000, 1, 1)) DTModel.objects.create(name='2', start_datetime=datetime(2001, 2, 5)) DTModel.objects.create(name='3', start_datetime=datetime(2002, 1, 7)) DTModel.objects.create(name='4', start_datetime=datetime(2003, 2, 8)) subquery_qs = DTModel.objects.filter( name__in=['3', '4'], start_datetime__month=ExtractMonth(OuterRef('start_datetime')), ) qs = DTModel.objects.filter(name__in=['1', '2']) qs = qs.annotate(related_name=Subquery(subquery_qs.values('name')[:1]))
This code raises where ExtractMonth(OuterRef('start_datetime')):
'ResolvedOuterRef' object has no attribute 'output_field'
It can be works with something like:
start_datetime__month=ExtractMonth(Func(OuterRef('start_datetime'), function='', output_field=DateTimeField())),
Or if redefine class:
class ExtractMonthCustom(ExtractMonth): def resolve_expression(self, *args, **kwargs): return super(Extract, self).resolve_expression(*args, **kwargs)
Change History (4)
comment:1 by , 3 years ago
Owner: | changed from | to
---|
comment:2 by , 3 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Note:
See TracTickets
for help on using tickets.
You can also use
ExpressionWrapper
for this purpose in the mean time.start_datetime__month=ExtractMonth(ExpressionWrapper(OuterRef('start_datetime'), output_field=DateTimeField()))