Package: django-range-merge
django-range-mergeEnables therange_mergeAggregate for Django on Postgres.range_merge"Computes the smallest range that includes ... the given ranges".This package should only be used with Django projects using the Postgres database. SeePostgres docs on Range Functions.Note: This app is still a work-in-progress, but currently works. Tests have not yet been implemented.InstallingAdd toINSTALLED_APPS:INSTALLED_APPS=[..."django_range_merge",...]Migrate to apply the aggregation to your database:>pythonmanage.pymigrateGetting StartedHere is a quick example. We have anEventmodel with two different range fields:period, which contains the datetime range period during which the Event occurs; andpotential_visitors, which is an approximation of the minimum and maximum number of people attending the Event.We want two different views to help Event organizers understand some aggregate details about Events.range_of_visitors_this_month: Show the overall lowest and greatest number of people we expect for all events this monthoverall_dates_of_funded_events: Shows the overall range of dates for Events which are funded (theis_fundedBooleanField is set to True)models.pyclassEvent(models.Model):name=models.CharField(max_length=30)period=models.DateTimeRangeField(help_text="The period of time this event covers")potential_visitors=models.IntegerRangeField(help_text="The range of visitors expected at this event")is_funded=BooleanField(default=False)classMeta:verbose_name="Event"verbose_name_plural="Events"def__str__(self):returnself.namedate_utils.py (get a range covering the entire current month)fromdjango.utilsimporttimezonefromdateutil.relativedeltaimportrelativedeltafrompsycopg2.extrasimportDateTimeTZRangedefget_month_range():"""Return a DateTimeRange range covering this entire month"""today=timezone.now().date()iftoday.day>25:today+=timezone.timedelta(7)this_month_start=today.replace(day=1)next_month_start=this_month_start+relativedelta(months=1)returnDateTimeTZRange(this_month_start,next_month_start)views.pyfromdjango.db.modelsimportF,Aggregatefromdjango.template.responseimportTemplateResponsefrom.date_utilsimportget_month_rangedefrange_of_visitors_this_month(request):"""e.g., given the following instances:{"id" : 1, "name" : "Birthday",     "potential_visitors" : "[2, 3)", ...}{"id" : 2, "name" : "Bake Sale",    "potential_visitors" : "[30, 50)", ...}{"id" : 3, "name" : "Band Camp",    "potential_visitors" : "[22, 28)", ...}{"id" : 4, "name" : "Cooking Show", "potential_visitors" : "[7, 20)", ...}{"id" : 5, "name" : "Pajama Day",   "potential_visitors" : "[15, 30)", ...}The result would be:{'output': NumericRange(2, 50, '[)')}"""template="base.html"context=Event.objects.filter(period__overlap=get_month_range()).aggregate(output=Aggregate(F("potential_visitors"),function="range_merge"))returnTemplateResponse(request,template,context)defoverall_dates_of_funded_events(request):template="base.html"context=Event.objects.filter(is_funded=True).aggregate(output=Aggregate(F("period"),function="range_merge"))# Example result: {'output': DateTimeRange("2022-10-01 02:00:00", "2022-12-07 12:00:00", '[)')}returnTemplateResponse(request,template,context)base.html<html><head></head><body>{{ output }}</body></html>LicenseThe code in this repository is licensed under The MIT License. See LICENSE.md in the repository for more details.ContributingContributions are very welcome.This project is currently accepting all types of contributions, bug fixes,
security fixes, maintenance work, or new features.  However, please make sure
to have a discussion about your new feature idea with the maintainers prior to
beginning development to maximize the chances of your change being accepted.
You can start a conversation by creating a new issue on this repo summarizing
your idea.