Skip to content

Using references inside Subquery #212

Description

@Dufran

Hello.
I'm trying to add last_activity timestamp on ORM level using subquery, but references only supports passing an object, that seems impossible to do.
Is there any way it's possible to use references inside subquery?(maybe with providing model class and PK?) ?

# * Can't be used
po_activities_subquery = (
            pghistory.models.Events.objects.across(
                po_event,
                po_line_item_event,
                po_note_event,
                po_document_event,
            )
            .references(
                PO.objects.all().filter(
                    id=OuterRef("pk"),
                ),
            )
            .order_by("-pgh_created_at")
            .values("pgh_created_at")[:1]
        )

# * Not working 
po_activities_subquery = (
            pghistory.models.Events.objects.across(
                po_event,
                po_line_item_event,
                po_note_event,
                po_document_event,
            )
            .references(
           OuterRef("pk"),
            )
            .order_by("-pgh_created_at")
            .values("pgh_created_at")[:1]
        )

po_base = PO.objects.all().annotate(
            document_type=Value(UserDocumentType.PURCHASE_ORDER.name),
            document_number=F("order_number"),
            last_activity=Subquery(
                po_activities_subquery,
            ),
        )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions