DecimalField causes inline formsets to disappear
|Reported by:||Owned by:||Cliff Dyer|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I have a client who typed non-numeric characters in a DecimalField (for price). When he saved the form returned but the inline formset had vanished.
I think when the characters came to the DecimalField, it raised an exception which caused the inline to crash and thus disappear.
def to_python(self, value): """ Validates that the input is a decimal number. Returns a Decimal instance. Returns None for empty values. Ensures that there are no more than max_digits in the number, and no more than decimal_places digits after the decimal point. """ if value in validators.EMPTY_VALUES: return None if self.localize: value = formats.sanitize_separators(value) value = smart_str(value).strip() try: value = Decimal(value) except DecimalException: # >>> i think this is the culprit, should it throw validationerrors here? # about line 292 raise ValidationError(self.error_messages['invalid']) return value
I know my client shouldn't have put characters in there of course, but it's very possible to accidentally put in wrong data in a form field. I think the decimal field should display an error rather than raising an exception at this point.
I'm using this for now:
class SafeDecimalField(forms.DecimalField): def to_python(self, value): try: return super(SafeDecimalField, self).to_python(value) except Exception, ex: return Decimal("0.0")
Change History (5)
comment:3 follow-up: 4 Changed 5 years ago by
|Owner:||changed from nobody to Cliff Dyer|
|Status:||new → assigned|