{{{#!python
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 a ModelForm with this
field.
--
Ticket URL: <https://code.djangoproject.com/ticket/30201>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> In a fresh Django installation:
>
>
> {{{#!python
> 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 a ModelForm with this
> field.
New description:
In a fresh Django installation:
{{{#!python
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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/30201#comment:1>
* status: new => closed
* resolution: => invalid
Comment:
`data` needs to be in the form that a `Form` would recieve it.
[https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/forms/#django.contrib.postgres.forms.RangeWidget
`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, '[)')}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30201#comment:2>
* component: Uncategorized => contrib.postgres
* type: Uncategorized => Bug
--
Ticket URL: <https://code.djangoproject.com/ticket/30201#comment:3>
Comment (by George Tantiras):
Thank you, it works!
I blindly followed the docs of
[https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/forms/#django.contrib.postgres.forms.SimpleArrayField
SimpleArrayField ] and tried to accordingly assign the value in the Form.
The thing is that neither
[https://docs.djangoproject.com/en/2.1/ref/forms/widgets/#django.forms.MultiWidget
MultiWidget] nor
[https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/forms/#django.contrib.postgres.forms.RangeWidget
RangeWidget] have any examples.
--
Ticket URL: <https://code.djangoproject.com/ticket/30201#comment:4>