#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 Matthew Schinckel):

 There's two parts: ensuring that the user-entered-data is upper-bounds-
 inclusive, and ensuring that the saved data is presented back to the user
 as upper-bounds-inclusive.

 The first part of this is trivial - just have a bounds value of '[]'
 passed in to the constructor. In my case, I have an InclusiveRangeMixin
 that does this in compress.

 The second part is less clear - because in the case of distinct range
 types there is a "unit value" that needs to be removed from the upper
 bounds, but in the case of continuous ranges, this is not possible (but in
 that case, postgres will not have normalised it, so it's all good).

 Anyway, code speaks louder than words here, so here's how I've approached
 this in the past:


 {{{
 class InclusiveRangeMixin(object):
     _unit_value = None

     def compress(self, values):
         range_value = super(InclusiveRangeMixin, self).compress(values)
         if range_value:
             return self.range_type(range_value.lower, range_value.upper,
 bounds='[]')

     def prepare_value(self, value):
         value = super(InclusiveRangeMixin, self).prepare_value(value)
         # We need to clean both fields.
         value = [field.clean(val) for field, val in zip(self.fields,
 value)]
         if value[1] is not None:
             value[1] = value[1] - self._unit_value
         return value


 class InclusiveDateRangeField(InclusiveRangeMixin, DateRangeField):
     _unit_value = datetime.timedelta(1)


 class InclusiveIntegerRangeField(InclusiveRangeMixin, IntegerRangeField):
     _unit_value = 1

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/27147#comment:5>
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.e1fb71f13201b31d5ae7a5637621f4b1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to