django-generate-series


Namedjango-generate-series JSON
Version 0.5.0 PyPI version JSON
download
home_pagehttps://github.com/jacklinke/django-generate-series/
SummaryUse Postgres' generate_series to create sequences with Django's ORM
upload_time2023-01-22 00:36:51
maintainer
docs_urlNone
authorJack Linke
requires_python>=3.8,<4.0
licenseApache-2.0
keywords sequence series set set-returning queryset
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-generate-series

Use Postgres' generate_series to create sequences with Django's ORM

https://django-generate-series.readthedocs.io/

## Goals

When using Postgres, the set-returning functions allow us to easily create sequences of numbers, dates, datetimes, etc. Unfortunately, this functionality is not currently available within the Django ORM.

This project makes it possible to create such sequences, which can then be used with Django QuerySets. For instance, assuming you have an Order model, you can create a set of sequential dates and then annotate each with the number of orders placed on that date. This will ensure you have no date gaps in the resulting QuerySet. To get the same effect without this package, additional post-processing of the QuerySet with Python would be required.

## Terminology

Although this packages is named django-generate-series based on Postgres' [`generate_series` set-returning function](https://www.postgresql.org/docs/current/functions-srf.html), mathematically we are creating a [sequence](https://en.wikipedia.org/wiki/Sequence) rather than a [series](https://en.wikipedia.org/wiki/Series_(mathematics)).

- **sequence**: Formally, "a list of objects (or events) which have been ordered in a sequential fashion; such that each member either comes before, or after, every other member."

    In django-generate-series, we can generate sequences of integers, decimals, dates, datetimes, as well as the equivalent ranges of each of these types.

- **term**: The *n*th item in the sequence, where '*n*th' can be found using the id of the model instance.

    This is the name of the field in the model which contains the term value.

## API

The package includes a `generate_series` function from which you can create your own series-generating QuerySets. The field type passed into the function as `output_field` determines the resulting type of series that can be created. (Thanks, [@adamchainz](https://twitter.com/adamchainz) for the format suggestion!)

### generate_series arguments

- ***start*** - The value at which the sequence should begin (required)
- ***stop*** - The value at which the sequence should end. For range types, this is the lower value of the final term (required)
- ***step*** - How many values to step from one term to the next. For range types, this is the step from the lower value of one term to the next. (required for non-integer types)
- ***span*** - For range types other than date and datetime, this determines the span of the lower value of a term and its upper value (optional, defaults to 1 if neeeded in the query)
- ***output_field*** - A django model field class, one of BigIntegerField, IntegerField, DecimalField, DateField, DateTimeField, BigIntegerRangeField, IntegerRangeField, DecimalRangeField, DateRangeField, or DateTimeRangeField. (required)
- ***include_id*** - If set to True, an auto-incrementing `id` field will be added to the QuerySet.
- ***max_digits*** - For decimal types, specifies the maximum digits
- ***decimal_places*** - For decimal types, specifies the number of decimal places
- ***default_bounds*** - In Django 4.1+, allows specifying bounds for list and tuple inputs. See [Django docs](https://docs.djangoproject.com/en/dev/releases/4.1/#django-contrib-postgres)

## Basic Examples

```python
# Create a bunch of sequential integers
integer_sequence_queryset = generate_series(
    0, 1000, output_field=models.IntegerField,
)

for item in integer_sequence_queryset:
    print(item.term)
```

Result:

    term
    ----
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ...
    1000

```python
# Create a sequence of dates from now until a year from now
now = timezone.now().date()
later = (now + timezone.timedelta(days=365))

date_sequence_queryset = generate_series(
    now, later, "1 days", output_field=models.DateField,
)

for item in date_sequence_queryset:
    print(item.term)
```

Result:

    term
    ----
    2022-04-27
    2022-04-28
    2022-04-29
    2022-04-30
    2022-05-01
    2022-05-02
    2022-05-03
    ...
    2023-04-27

*Note: See [the docs](https://django-generate-series.readthedocs.io/en/latest/usage_examples.html) and the example project in the tests directory for further examples of usage.*

## Usage with partial

If you often need sequences of a given field type or with certain args, you can use [partial](https://docs.python.org/3/library/functools.html#functools.partial).

Example with default `include_id` and `output_field` values:

```python
from functools import partial

int_and_id_series = partial(generate_series, include_id=True, output_field=BigIntegerField)

qs = int_and_id_series(1, 100)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jacklinke/django-generate-series/",
    "name": "django-generate-series",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "sequence,series,set,set-returning,queryset",
    "author": "Jack Linke",
    "author_email": "jack@watervize.com",
    "download_url": "https://files.pythonhosted.org/packages/d3/52/48f0d808f22742e91f13f36292709b070bd4ee5948db80b5ef8489c31164/django_generate_series-0.5.0.tar.gz",
    "platform": null,
    "description": "# django-generate-series\n\nUse Postgres' generate_series to create sequences with Django's ORM\n\nhttps://django-generate-series.readthedocs.io/\n\n## Goals\n\nWhen using Postgres, the set-returning functions allow us to easily create sequences of numbers, dates, datetimes, etc. Unfortunately, this functionality is not currently available within the Django ORM.\n\nThis project makes it possible to create such sequences, which can then be used with Django QuerySets. For instance, assuming you have an Order model, you can create a set of sequential dates and then annotate each with the number of orders placed on that date. This will ensure you have no date gaps in the resulting QuerySet. To get the same effect without this package, additional post-processing of the QuerySet with Python would be required.\n\n## Terminology\n\nAlthough this packages is named django-generate-series based on Postgres' [`generate_series` set-returning function](https://www.postgresql.org/docs/current/functions-srf.html), mathematically we are creating a [sequence](https://en.wikipedia.org/wiki/Sequence) rather than a [series](https://en.wikipedia.org/wiki/Series_(mathematics)).\n\n- **sequence**: Formally, \"a list of objects (or events) which have been ordered in a sequential fashion; such that each member either comes before, or after, every other member.\"\n\n    In django-generate-series, we can generate sequences of integers, decimals, dates, datetimes, as well as the equivalent ranges of each of these types.\n\n- **term**: The *n*th item in the sequence, where '*n*th' can be found using the id of the model instance.\n\n    This is the name of the field in the model which contains the term value.\n\n## API\n\nThe package includes a `generate_series` function from which you can create your own series-generating QuerySets. The field type passed into the function as `output_field` determines the resulting type of series that can be created. (Thanks, [@adamchainz](https://twitter.com/adamchainz) for the format suggestion!)\n\n### generate_series arguments\n\n- ***start*** - The value at which the sequence should begin (required)\n- ***stop*** - The value at which the sequence should end. For range types, this is the lower value of the final term (required)\n- ***step*** - How many values to step from one term to the next. For range types, this is the step from the lower value of one term to the next. (required for non-integer types)\n- ***span*** - For range types other than date and datetime, this determines the span of the lower value of a term and its upper value (optional, defaults to 1 if neeeded in the query)\n- ***output_field*** - A django model field class, one of BigIntegerField, IntegerField, DecimalField, DateField, DateTimeField, BigIntegerRangeField, IntegerRangeField, DecimalRangeField, DateRangeField, or DateTimeRangeField. (required)\n- ***include_id*** - If set to True, an auto-incrementing `id` field will be added to the QuerySet.\n- ***max_digits*** - For decimal types, specifies the maximum digits\n- ***decimal_places*** - For decimal types, specifies the number of decimal places\n- ***default_bounds*** - In Django 4.1+, allows specifying bounds for list and tuple inputs. See [Django docs](https://docs.djangoproject.com/en/dev/releases/4.1/#django-contrib-postgres)\n\n## Basic Examples\n\n```python\n# Create a bunch of sequential integers\ninteger_sequence_queryset = generate_series(\n    0, 1000, output_field=models.IntegerField,\n)\n\nfor item in integer_sequence_queryset:\n    print(item.term)\n```\n\nResult:\n\n    term\n    ----\n    0\n    1\n    2\n    3\n    4\n    5\n    6\n    7\n    8\n    9\n    10\n    ...\n    1000\n\n```python\n# Create a sequence of dates from now until a year from now\nnow = timezone.now().date()\nlater = (now + timezone.timedelta(days=365))\n\ndate_sequence_queryset = generate_series(\n    now, later, \"1 days\", output_field=models.DateField,\n)\n\nfor item in date_sequence_queryset:\n    print(item.term)\n```\n\nResult:\n\n    term\n    ----\n    2022-04-27\n    2022-04-28\n    2022-04-29\n    2022-04-30\n    2022-05-01\n    2022-05-02\n    2022-05-03\n    ...\n    2023-04-27\n\n*Note: See [the docs](https://django-generate-series.readthedocs.io/en/latest/usage_examples.html) and the example project in the tests directory for further examples of usage.*\n\n## Usage with partial\n\nIf you often need sequences of a given field type or with certain args, you can use [partial](https://docs.python.org/3/library/functools.html#functools.partial).\n\nExample with default `include_id` and `output_field` values:\n\n```python\nfrom functools import partial\n\nint_and_id_series = partial(generate_series, include_id=True, output_field=BigIntegerField)\n\nqs = int_and_id_series(1, 100)\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Use Postgres' generate_series to create sequences with Django's ORM",
    "version": "0.5.0",
    "project_urls": {
        "Documentation": "https://django-generate-series.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/jacklinke/django-generate-series/",
        "Repository": "https://github.com/jacklinke/django-generate-series/"
    },
    "split_keywords": [
        "sequence",
        "series",
        "set",
        "set-returning",
        "queryset"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7fa6e046d514139eec2b0d534a499a5bee5eb735e45b528113bf5cd7cdf87a2d",
                "md5": "256c206220fe67b5ea37ac7ee6a137b1",
                "sha256": "54e33e5aba69be75f591bda970421dee9f1c5feeb84c20d8cee634bcc0e249bc"
            },
            "downloads": -1,
            "filename": "django_generate_series-0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "256c206220fe67b5ea37ac7ee6a137b1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 11444,
            "upload_time": "2023-01-22T00:36:49",
            "upload_time_iso_8601": "2023-01-22T00:36:49.903730Z",
            "url": "https://files.pythonhosted.org/packages/7f/a6/e046d514139eec2b0d534a499a5bee5eb735e45b528113bf5cd7cdf87a2d/django_generate_series-0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d35248f0d808f22742e91f13f36292709b070bd4ee5948db80b5ef8489c31164",
                "md5": "7dd47ebb1d56ff29cbb2fe9bf317b9f2",
                "sha256": "8cced6473ba75aed5e1e2ecd6f5426d11d33926e86d2630dabe9c424b7a6da8a"
            },
            "downloads": -1,
            "filename": "django_generate_series-0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "7dd47ebb1d56ff29cbb2fe9bf317b9f2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 13208,
            "upload_time": "2023-01-22T00:36:51",
            "upload_time_iso_8601": "2023-01-22T00:36:51.135777Z",
            "url": "https://files.pythonhosted.org/packages/d3/52/48f0d808f22742e91f13f36292709b070bd4ee5948db80b5ef8489c31164/django_generate_series-0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-22 00:36:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jacklinke",
    "github_project": "django-generate-series",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "django-generate-series"
}
        
Elapsed time: 0.38785s