# Proposal: Add generate\_series() support to contrib.postgres?

**URL:** <https://forum.djangoproject.com/t/proposal-add-generate-series-support-to-contrib-postgres/21947>\
**Category:** ORM\
**Created:** [June 29, 2023, 1:01pm UTC](https://forum.djangoproject.com/t/proposal-add-generate-series-support-to-contrib-postgres/21947 "2023-06-29T13:01:34Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [July 12, 2023, 2:52am UTC](https://forum.djangoproject.com/t/proposal-add-generate-series-support-to-contrib-postgres/21947/4 "2023-07-12T02:52:27Z")

</div>

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

```python
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=(...)
        )
    )
)

```

---

_[View the full topic](https://forum.djangoproject.com/t/proposal-add-generate-series-support-to-contrib-postgres/21947)._
