marksweb / django-bleach

Bleach is a Python module that takes any HTML input, and returns valid, sanitised HTML that contains only an allowed subset of HTML tags, attributes and styles. django-bleach is a Django app that makes using bleach extremely easy.
MIT License
148 stars 23 forks source link

Form field doesn't respect empty_value argument #28

Closed MrkGrgsn closed 3 years ago

MrkGrgsn commented 3 years ago

Describe the bug The form field always returns None if no value was submitted and if it's for a model field with null=False the DB raises an IntegrityError.

To Reproduce

class MyModel(Model):
    bleached = models.BleachField(blank=True, null=False)

class MyModelForm(ModelForm):
    class Meta:
        model = MyModel
        fields = "_all__"

form = MyModelForm(data={}, instance=my_model)
form.save()

Expected behavior I expect that BleachField.to_python returns self.empty_value if the value is considered empty.

The empty_value argument controls whether CharField objects return None or an empty string when no value was provided by the user and, for model forms, ensures that the field returns the correct value for the model field configuration and chosen DB.