#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 )
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 , 7 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 7 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
comment:3 by , 7 years ago
| Component: | Uncategorized → contrib.postgres |
|---|---|
| Type: | Uncategorized → Bug |
comment:4 by , 7 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.
dataneeds to be in the form that aFormwould recieve it. `RangeWidget` is based off-of `MultiWidget` so it’s expecting a_0,_1pair, rather than aNumericRange:>>> 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, '[)')}