#16328: FilePathField should include blank option even when required=True
----------------------------+------------------------------------------
     Reporter:  ringemup@…  |                    Owner:  Mahmoud Nasser
         Type:  Bug         |                   Status:  new
    Component:  Forms       |                  Version:  1.3
     Severity:  Normal      |               Resolution:
     Keywords:              |             Triage Stage:  Accepted
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  1
----------------------------+------------------------------------------
Comment (by Mahmoud Nasser):

 In the `FilePathField` we have the following line of code:

 {{{
         if self.required:
             self.choices = []
         else:
             self.choices = [("", "---------")]
 }}}

 and when we pass the parmater from the model to the form we assign the
 `required` parameter based on `blank` that comming from the model as you
 could see here:


 {{{
     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,
         }
       ......
 }}}

 What I am planing to do is to cover this lines of code in the unittests as
 you could see in this commit:
 https://github.com/django/django/commit/a64a61bf4a which made the change
 in the `FilePathField` as I mentioned above , I will add tests for that
 change like this in `test_filepathfield.py` to confirm that it is working
 fine:


 {{{
     def test_should_include_blank_option_when_required_is_true(self):
         f = FilePathField(path=self.path, match=r"^.*?\.py$",
 required=False)
         self.assertChoices(f, [('', '---------')] +
 self.expected_choices[:4])
 }}}


 if it is ok I will do a PR for above change.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/16328#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070197b77ff56a-451bc87c-c4e0-45d9-b0bc-925da923ca8f-000000%40eu-central-1.amazonses.com.

Reply via email to