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 Chiorufarewerin, 3 years ago

Owner: changed from nobody to Chiorufarewerin

comment:2 by Simon Charette, 3 years ago

Triage Stage: UnreviewedAccepted

It can be works with something like:

start_datetime__month=ExtractMonth(Func(OuterRef('start_datetime'), function='', output_field=DateTimeField())),

You can also use ExpressionWrapper for this purpose in the mean time.

start_datetime__month=ExtractMonth(ExpressionWrapper(OuterRef('start_datetime'), output_field=DateTimeField()))

Last edited 3 years ago by Simon Charette (previous) (diff)

comment:3 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 3954bf50:

Fixed #32750 -- Fixed crash of Extract() transform on OuterRef() expressions.

Thanks Simon Charette for the review.

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