Hi Brylie, Laurence,

I think it should be possible to do this using my Check Constraints work
(https://github.com/django/django/pull/7615) or something built on top
of it.

I plan to get this work into shape so it can land in Django 2.1.

I hope this helps,
Ian

On 03/10/17 10:40, Laurence Sonnenberg wrote:
> A pythonic way to do this would be extremely useful if it existed. As
> you suggested, I did this using a sql exclusion constraint statement on
> the table and a migration.
>  
> For example:
> 
> 000x_your_migration_name.py
> 
>  # -*- coding: utf-8 -*-
> from __future__ import unicode_literals
> 
> from django.db import migrations
> 
> SQL = """ALTER TABLE your_table DROP CONSTRAINT IF EXISTS
> range_overlap_exclusion;
>                ALTER TABLE your_table ADD CONSTRAINT range_overlap_exclusion
>                EXCLUDE USING gist (resource WITH =, date_range WITH &&);"""
> 
> 
> class Migration(migrations.Migration):
> 
>     dependencies = [
>         ('module_name', '000x_previous_migration'),
>     ]
> 
>     operations = [migrations.RunSQL(sql=SQL,
> reverse_sql=migrations.RunSQL.noop), ]
> 
> 
> On Sunday, October 1, 2017 at 11:53:53 PM UTC+2, Brylie Christopher
> Oxley wrote:
> 
>     Now that Django supports the DateRangeField, is there a 'Pythonic'
>     way to prevent records from having overlapping date ranges?
> 
>     # Hypothetical use case
>     One hypothetical use case would be a booking system, where you don't
>     want people to book the same resource at the same time.
> 
>     # Hypothetical example code
>     <!-- language: lang-py -->
> 
>     class Booking(models.model):
>     # The resource to be reserved
>     resource = models.ForeignKey('Resource')
>     # When to reserve the resource
>     date_range = models.DateRangeField()
> 
>     class Meta:
>     unique_together = ('resource', 'date_range',)
> 
>     This might have the effect of adding an exclusion constraint on the
>     underlying table:
>     
> https://www.postgresql.org/docs/9.3/static/sql-createtable.html#SQL-CREATETABLE-EXCLUDE
>     
> <https://www.postgresql.org/docs/9.3/static/sql-createtable.html#SQL-CREATETABLE-EXCLUDE>
>     -- 
>     Sent from my Android device with K-9 Mail. Please excuse my brevity.
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/f595965d-8646-40f8-8637-f5011ba8f535%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/f595965d-8646-40f8-8637-f5011ba8f535%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/837bbc77-dd7f-b3d5-d447-45d1613d10db%40feete.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to