django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Fix #7722.

Open tbrknt opened this issue 3 years ago • 9 comments

Render BooleanFields with allow_null=True as HTML select rather than as HTML checkbox in Browsable API #7722.

Description

This PR is created in order to fix issue #7722. A serializer field of type BooleanField with allow_null=True was being rendered as HTML checkbox elements on Browsable API forms even though three values (True, False, and None/null) are possible. It was also happening for the model BooleanField instances with allow_null=True that are converted to serializer BooleanField instances. In this PR, I worked on keeping the checkbox rendering for BooleanFields with allow_null=False while providing dropdown rendering (HTML select element) for BooleanFields with allow_null=True.

fix #7722 refs #7722

tbrknt avatar Mar 29 '21 14:03 tbrknt

Thanks for working on this. Having a look over, I'm wondering about the alternative of not introducing a new template, and using the existing "select.html" template.

tomchristie avatar Mar 29 '21 14:03 tomchristie

Hi @tomchristie , Thank you for your comment. I was thinking about that alternative too. The following part from select.html was my main hesitation to use it. I was not sure if I should adapt the value of BooleanField and that part of the template. Without changing that template, I could not think of a solution to drop -------- and introduce unknown option. What do you think about it? If unknown option is not that important, I believe it is possible to make the value of the BoundField as 'False' string instead of False boolean value. That way, for False, we can avoid if not field.value.

  <select class="form-control" name="{{ field.name }}">
    {% if field.allow_null or field.allow_blank %}
      <option value="" {% if not field.value %}selected{% endif %}>--------</option>
    {% endif %}
  </select>

tbrknt avatar Mar 29 '21 15:03 tbrknt

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 29 '22 19:04 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 21 '23 14:01 stale[bot]

Could the PR title have some words in it to describe the bug or the fix? Would help all of us looking at notifications or lists of tickets. Thanks!

merwok avatar Jan 21 '23 23:01 merwok

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 25 '23 05:03 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 11 '23 22:06 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 12 '23 04:08 stale[bot]

we might need a new taker for this

auvipy avatar Aug 13 '23 06:08 auvipy