Tools for building, querying, manipulating, and exporting directed graphs with django
Project description
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.
Models
The package includes a get_series_model
function from which you can create your own series-generating models in your project's models.py file. The field type passed into the function determines the resulting type of series that can be created.
Canonical examples for each supported series type:
class IntegerTest(get_series_model(models.IntegerField)):
# Creates a model for generating Integer series
pass
class DecimalTest(get_series_model(models.DecimalField, max_digits=9, decimal_places=2)):
# Creates a model for generating Decimal series
pass
class DateTest(get_series_model(models.DateField)):
# Creates a model for generating Date series
pass
class DateTimeTest(get_series_model(models.DateTimeField)):
# Creates a model for generating DateTime series
pass
You can also create sequences of ranges.
class IntegerRangeTest(get_series_model(IntegerRangeField)):
# Creates a model for generating Integer range series
pass
class DecimalRangeTest(get_series_model(DecimalRangeField)):
# Creates a model for generating Decimal range series
pass
class DateRangeTest(get_series_model(DateRangeField)):
# Creates a model for generating Date range series
pass
class DateTimeRangeTest(get_series_model(DateTimeRangeField)):
# Creates a model for generating DateTime range series
pass
Note: See the docs and the example project in the tests directory for further examples of usage.
API
# 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:
0
1
2
3
4
5
6
7
8
9
10
...
1000
# 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:
2022-04-27
2022-04-28
2022-04-29
2022-04-30
2022-05-01
2022-05-02
2022-05-03
...
2023-04-27
Terminology
Although this packages is named django-generate-series based on Postgres' generate_series
set-returning function, mathematically we are creating a sequence rather than a series.
-
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 nth item in the sequence, where 'nth' 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.
History
0.4.0 (2022-04-27)
Breaking changes
- Modify API taking into consideration recommendations from Adam Johnson.
0.3.0 (2022-04-25)
Breaking changes
- Changed from using
id
as the sequence value to using theterm
field.
0.2.1 (2022-04-25)
- Implemented all PRs from adamchainz.
- Improve test matrix.
- Cleanup unused code and comments.
0.2.0 (2022-04-23)
- Basic package functionality is implemented.
- Tests have been added.
- Initial documentation is added.
0.1.0 (2022-02-08)
- Built initial readme entry to start documenting project goals.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-generate-series-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36d92b821c1c660bc295eb2e44c8441aaae795b2dbced37b9ad621782fd221f7 |
|
MD5 | e631d5976f5b39df154774239feae812 |
|
BLAKE2b-256 | db93a42a2b2fe1b3635406ab300135f086bb67ffac2d869fefe43c204ca7ba36 |
Hashes for django_generate_series-0.4.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ce998918ec3373ad7ea9c77654429f6187709c83fe5c03dbbd73fbcc04fb407 |
|
MD5 | 69e2ffd1e2e219edfcce0159323e8622 |
|
BLAKE2b-256 | fe086f7252e394e0dd9f3e9ba7c772260ac2b57fb9eb0b28f537efa20cbee1b0 |