#27147: Add support for defining bounds in postgres range fields
-------------------------------------+-------------------------------------
     Reporter:  Kirill Stepanov      |                    Owner:  (none)
         Type:  New feature          |                   Status:  new
    Component:  contrib.postgres     |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  postgres range       |             Triage Stage:  Accepted
  bounds                             |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Jakub SkaƂecki):

 In case anyone has this problem, I've prepared a fix. Of course better if
 appropriate fields natively supported `bounds` argument, but this is a
 drop-in replacement:

 {{{
 # db_utils.py
 from functools import partial, wraps
 from django.contrib.postgres.fields import ranges


 def create_bounded_range_field(cls):
     def init_wrapper(f):
         @wraps(f)
         def __init__(self, *args, **kwargs):
             if 'bounds' in kwargs:
                 self.range_type = partial(self.range_type,
 bounds=kwargs.pop('bounds'))
             f(self, *args, **kwargs)
         return __init__
     cls.__init__ = init_wrapper(cls.__init__)
     return cls


 DateRangeField = create_bounded_range_field(ranges.DateRangeField)
 DateTimeRangeField = create_bounded_range_field(ranges.DateTimeRangeField)
 BigIntegerRangeField =
 create_bounded_range_field(ranges.BigIntegerRangeField)
 FloatRangeField = create_bounded_range_field(ranges.FloatRangeField)
 IntegerRangeField = create_bounded_range_field(ranges.IntegerRangeField)

 }}}

 Not it's possible to declare field like this:

 {{{
 from db_utils import DateRangeField

 class MyModel(Model):
     dates = DateRangeField(bounds='[]')
 }}}

 If this solution has drawbacks, let me know.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/27147#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.e6d910e4f0a41d2465856b6e7343c8f8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to