from ....fields import DecimalField
from .widgets import BRDecimalInput

from decimal import Decimal


class BRDecimalField(DecimalField):
    widget = BRDecimalInput

    def to_python(self, value):
        value = value.replace(',', '.')
        value = value.replace('.', '', value.count('.')-1)

        return Decimal(value)

from django.forms import ValidationError

from ....fields import DecimalField
from .widgets import BRDecimalInput

from decimal import Decimal, DecimalException


class BRDecimalField(DecimalField):
    widget = BRDecimalInput

    def to_python(self, value):
        value = value.replace(',', '.')
        value = value.replace('.', '', value.count('.') - 1)

        try:
            value = Decimal(value)
        except DecimalException:
            raise ValidationError(self.error_messages['invalid'])

Fix a bug while handling invalid values
Fix a bug while handling invalid values

+ from django.forms import ValidationError
+ 
  from ....fields import DecimalField
  from .widgets import BRDecimalInput
  
- from decimal import Decimal
+ from decimal import Decimal, DecimalException
  
  
  class BRDecimalField(DecimalField):
      widget = BRDecimalInput
  
      def to_python(self, value):
          value = value.replace(',', '.')
-         value = value.replace('.', '', value.count('.')-1)
+         value = value.replace('.', '', value.count('.') - 1)
  
+         try:
-         return Decimal(value)
+             value = Decimal(value)
+         except DecimalException:
+             raise ValidationError(self.error_messages['invalid'])
  
Fix a bug while handling invalid values
## Code Before:
from ....fields import DecimalField
from .widgets import BRDecimalInput

from decimal import Decimal


class BRDecimalField(DecimalField):
    widget = BRDecimalInput

    def to_python(self, value):
        value = value.replace(',', '.')
        value = value.replace('.', '', value.count('.')-1)

        return Decimal(value)

## Instruction:
Fix a bug while handling invalid values
## Code After:
from django.forms import ValidationError

from ....fields import DecimalField
from .widgets import BRDecimalInput

from decimal import Decimal, DecimalException


class BRDecimalField(DecimalField):
    widget = BRDecimalInput

    def to_python(self, value):
        value = value.replace(',', '.')
        value = value.replace('.', '', value.count('.') - 1)

        try:
            value = Decimal(value)
        except DecimalException:
            raise ValidationError(self.error_messages['invalid'])
