Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model formfield() does not honor model field blank attribute making form field always required #21

Closed
miki725 opened this issue Jul 19, 2021 · 1 comment

Comments

@miki725
Copy link

miki725 commented Jul 19, 2021

Describe the bug

When blank=True is set to True, expected field.formfield() should be required=False however that is not honored.

To Reproduce

Steps to reproduce the behavior:

  1. Create model
  2. Add bleached field with blank=True
  3. Add it to django admin
  4. Check in django admin if field is required

Expected behavior

It should appear as not required but it appears as required

Context

Looks like its related to

if not self.choices:
return forms.BleachField(
label=self.verbose_name,
max_length=self.max_length,
allowed_tags=self.bleach_kwargs.get("tags"),
allowed_attributes=self.bleach_kwargs.get("attributes"),
allowed_styles=self.bleach_kwargs.get("styles"),
allowed_protocols=self.bleach_kwargs.get("protocols"),
strip_tags=self.bleach_kwargs.get("strip"),
strip_comments=self.bleach_kwargs.get("strip_comments"),
)

Recently it was changed to return BleachField which inherently does not call super() where base Field correctly interprets other django model field attributes such as blank:

https://github.com/django/django/blob/56f9579105c324ff15250423bf9f8bdf1634cfb4/django/db/models/fields/__init__.py#L908-L914

(copy from link since it does not render inline)

    def formfield(self, form_class=None, choices_form_class=None, **kwargs):
        """Return a django.forms.Field instance for this field."""
        defaults = {
            'required': not self.blank,
            'label': capfirst(self.verbose_name),
            'help_text': self.help_text,
        }
@marksweb
Copy link
Owner

marksweb commented Jul 19, 2021

Good catch @miki725 thanns for raising 👍

Are you able to contribute a fix for this?

I've added a couple of tests to check the required flag on 2 fields. Will see if I have chance to fix.

marksweb added a commit that referenced this issue Jul 19, 2021
marksweb added a commit that referenced this issue Jul 23, 2021
@marksweb marksweb mentioned this issue Jul 23, 2021
3 tasks
marksweb added a commit that referenced this issue Jul 23, 2021
* Fix for #21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants