Skip to content

Commit

Permalink
Respect form_class argument (#26)
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
MrkGrgsn committed Sep 12, 2021
1 parent 93540f8 commit 527c1c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
7 changes: 3 additions & 4 deletions django_bleach/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def __init__(self, allowed_tags=None, allowed_attributes=None,
if strip_comments:
self.bleach_kwargs["strip_comments"] = strip_comments

def formfield(self, **kwargs):
def formfield(self, form_class=forms.BleachField, **kwargs):
""" Makes the field for a ModelForm """

# If field doesn't have any choice return BleachField
# If field doesn't have any choices add kwargs expected by BleachField.
if not self.choices:
kwargs.update({
"max_length": self.max_length,
Expand All @@ -44,9 +44,8 @@ def formfield(self, **kwargs):
"strip_comments": self.bleach_kwargs.get("strip_comments"),
"required": not self.blank,
})
return forms.BleachField(**kwargs)

return super(BleachField, self).formfield(**kwargs)
return super(BleachField, self).formfield(form_class=form_class, **kwargs)

def pre_save(self, model_instance, add):
data = getattr(model_instance, self.attname)
Expand Down
26 changes: 26 additions & 0 deletions django_bleach/tests/test_modelformfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,29 @@ def test_required_field(self):
Check for the required flag on fields
"""
self.assertEqual(self.form_field.required, True)


class CustomBleachedFormField(bleach_forms.BleachField):
...


class OverriddenBleachContentModelForm(forms.ModelForm):
class Meta:
model = BleachContent
fields = '__all__'
field_classes = {
"content": CustomBleachedFormField,
}


class TestModelFormFieldOverrides(TestCase):

def setUp(self):
model_form = OverriddenBleachContentModelForm()
self.form_field = model_form.fields['content']

def test_formfield_type(self):
"""
Check content's form field is instance of CustomBleachedFormField.
"""
self.assertIsInstance(self.form_field, CustomBleachedFormField)

0 comments on commit 527c1c9

Please sign in to comment.