[Django] #30201: Form with IntegerRangeField does not validate

2 views
Skip to first unread message

Django

unread,
Feb 22, 2019, 7:53:29 AM2/22/19
to django-...@googlegroups.com
#30201: Form with IntegerRangeField does not validate
-------------------------------------------+------------------------
Reporter: George Tantiras | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
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.

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

Django

unread,
Feb 22, 2019, 9:16:29 AM2/22/19
to django-...@googlegroups.com
#30201: Form with IntegerRangeField does not validate
---------------------------------+--------------------------------------

Reporter: George Tantiras | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by George Tantiras:

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>

Django

unread,
Feb 22, 2019, 9:35:17 AM2/22/19
to django-...@googlegroups.com
#30201: Form with IntegerRangeField does not validate
---------------------------------+--------------------------------------

Reporter: George Tantiras | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* 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>

Django

unread,
Feb 22, 2019, 9:48:05 AM2/22/19
to django-...@googlegroups.com
#30201: Form with IntegerRangeField does not validate
----------------------------------+--------------------------------------

Reporter: George Tantiras | Owner: nobody
Type: Bug | Status: closed
Component: contrib.postgres | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => contrib.postgres
* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/30201#comment:3>

Django

unread,
Feb 22, 2019, 10:54:27 AM2/22/19
to django-...@googlegroups.com
#30201: Form with IntegerRangeField does not validate
----------------------------------+--------------------------------------

Reporter: George Tantiras | Owner: nobody
Type: Bug | Status: closed
Component: contrib.postgres | Version: 2.1
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages