Opened 6 years ago
Last modified 6 years ago
#29656 closed Bug
Range Fields do not support blank values via ModelForm — at Version 1
Reported by: | James Addison | Owned by: | nobody |
---|---|---|---|
Component: | contrib.postgres | Version: | dev |
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 )
The filing of this issue is based on a discussion in IRC (see https://botbot.me/freenode/django/2018-08-09/?msg=103129716&page=12 and prior messages), followed up by creating a test project to reproduce the issue.
Please do correct me if I am misusing range fields.
Saving a modelform with a model's rangefield 2 inputs left empty triggers an DB integrity error. I think the culprit lies with empty_values
not containing ['', '']
as a possible empty value. (see the code around https://github.com/django/django/blob/1.11.15/django/forms/fields.py#L1026).
self.compress([])
should return the result of self.range_type(None, None)
instead of just None
.
With a view like:
def home(request): if request.method == 'POST': form = RangeTestForm(request.POST) if form.is_valid(): instance = form.save() else: form = RangeTestForm(request.POST) return render(request, 'rangefieldtest/home.html', {'form': form})
Form like:
class RangeTestForm(forms.ModelForm): class Meta: model = RangeTest fields = '__all__'
and Model like:
from django.contrib.postgres.fields import FloatRangeField from psycopg2._range import NumericRange class RangeTest(models.Model): name = models.CharField(max_length=50, blank=True, default='') age_range = FloatRangeField(blank=True, default=NumericRange)
I will attach a sample project demonstrating this (use runserver, load the home page, click save)
psql table definition:
Table "public.rangefieldtest_rangetest" Column | Type | Modifiers -----------+-----------------------+----------------------------------------------------------------------- id | integer | not null default nextval('rangefieldtest_rangetest_id_seq'::regclass) name | character varying(50) | not null age_range | numrange | not null Indexes: "rangefieldtest_rangetest_pkey" PRIMARY KEY, btree (id)
data successfully stored on save (when at least one of the rangefield's 2 inputs are filled in):
id | name | age_range ----+------+----------- 2 | | [3.0,) (1 row)
Change History (2)
by , 6 years ago
Attachment: | rangefield.tgz added |
---|
comment:1 by , 6 years ago
Description: | modified (diff) |
---|
sample project reproducing the issue