Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30201 closed Bug (invalid)

Form with IntegerRangeField does not validate

Reported by: George Tantiras Owned by: nobody
Component: contrib.postgres Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by George Tantiras)

In a fresh Django installation:

from django import forms
from django.contrib.postgres.forms import IntegerRangeField
from psycopg2.extras import NumericRange


class A(forms.Form):
    lifespan = IntegerRangeField(required=True)


>>> data = {'lifespan': NumericRange(1918, 1999)} 
>>> p = A(data=data)
>>> p.data
{'lifespan': NumericRange(1918, 1999, '[)')}
>>> p.is_valid()
False
>>> p.errors
{'lifespan': ['This field is required.']}
>>> p.cleaned_data
{}

I discovered this behaviour while trying to test the clean method of a ModelForm with this field.

Change History (4)

comment:1 by George Tantiras, 6 years ago

Description: modified (diff)

comment:2 by Carlton Gibson, 6 years ago

Resolution: invalid
Status: newclosed

data needs to be in the form that a Form would recieve it. `RangeWidget` is based off-of `MultiWidget` so it’s expecting a _0, _1 pair, rather than a NumericRange:

>>> from django import forms
>>> from django.contrib.postgres.forms import IntegerRangeField
>>> from psycopg2.extras import NumericRange
>>> class A(forms.Form):
...     lifespan = IntegerRangeField(required=True)
...
>>> data = {"lifespan_0": 1918, "lifespan_1": 1999}
>>> p = A(data=data)
>>> p.data
{'lifespan_0': 1918, 'lifespan_1': 1999}
>>> p.is_valid()
True
>>> p.errors
{}
>>> p.cleaned_data
{'lifespan': NumericRange(1918, 1999, '[)')}

comment:3 by Tim Graham, 6 years ago

Component: Uncategorizedcontrib.postgres
Type: UncategorizedBug

comment:4 by George Tantiras, 6 years ago

Thank you, it works!

I blindly followed the docs of SimpleArrayField and tried to accordingly assign the value in the Form.

The thing is that neither MultiWidget nor RangeWidget have any examples.

Note: See TracTickets for help on using tickets.
Back to Top