Proposal: Add generate_series() support to contrib.postgres?

Hello David!

As Adam alluded at I think the bits that Django should focus on is providing the proper primitives for table-like and set returning expressions to be inserted and referenced through the ORM.

generate_series is an example of such expressions but unnest and the more commonly requested and database agnostic Subquery is another example of that.

If we put the problem of composite output fields aside for a moment (expressions that can return rows) I think that good first step would be to add an Expression flag that identify which expressions are set returning and use Subquery as the default one to validate the approach.

With this new metadata available we could deprecate FilteredRelation in favour of a more generic Relation expression that accepts either a resolvable relation name (like FilteredRelation does) or a set returning expression as it’s first argument.

It could then be used as

series = GenerateSeries(
    start=datetime(2000, 1, 1, tzinfo=utc),
    stop=datetime(2000, 1, 5, tzinfo=utc),
    step=timedelta(days=1)
)
Data.objects.alias(
    series=Relation(
        series,
        condition=Q(
            series__between=(...)
        )
    )
)
2 Likes