Discussion: ModelForms not allowing unbounded RangeField values

54 views
Skip to first unread message

James Addison

unread,
Aug 16, 2018, 4:06:44 PM8/16/18
to Django developers (Contributions to Django itself)
For others getting up to speed, prior discussion is here: https://code.djangoproject.com/ticket/29656.  Tim's comment from the ticket commentary is most instructive, I think:
 
I'm not sure if there's a model field in Django where there's a chance for two values that could be considered "empty" besides CharField and the range fields. The docs for Field.null say, "In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL."

Given model fields: 

text_field = CharField(blank=True, default='')
range_field
= FloatRangeField(blank=True, default=NumericRange)

The `CharField` (or a `TextField`) has similar parameters to the `RangeField`. For the `CharField`, a ModelForm correctly uses the default value to save '' (avoids raising an `IntegrityError`), but the RangeField does not (raises an `IntegrityError`).

I'll let the team discuss; I'm not sure that I can add much else. Please correct me if/where I'm wrong.

Carlton Gibson

unread,
Aug 17, 2018, 6:25:08 AM8/17/18
to Django developers (Contributions to Django itself)
Hey James, 

Thanks for your follow-up here. 

I think for me the thing that's compelling is the flow you describe in comment 12:

* A user renders an unbound model form, where all model fields have defaults. 
* The form is pre-filled with the default values. 
* The form is submitted without changes. (The default values are submitted.)
* A new model instance is created with the defaults in place. 

Except that doesn't work with range fields. 

In your case, where you've specified a default (and null=False) it makes sense that the form field would return
a `self.range_type(None, None)` rather than None here. 

What I'm not sure about is whether that always makes sense? 

What if I don't define that default on the model field? What if I want `None`? 

forms.CharField will always return it's empty_value here (by default `''`) but this is independent of the model default. 

Q: Do we want the same sort of behaviour for (forms) RangeFields? 
(Possibly we do...?)


C.
Reply all
Reply to author
Forward
0 new messages