Skip to content

Commit

Permalink
Fixed #8422: FilePathField now respects required=False.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10447 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Apr 8, 2009
1 parent 98ef7e8 commit a64a61b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion django/forms/fields.py
Expand Up @@ -829,9 +829,15 @@ def __init__(self, path, match=None, recursive=False, required=True,
super(FilePathField, self).__init__(choices=(), required=required,
widget=widget, label=label, initial=initial, help_text=help_text,
*args, **kwargs)
self.choices = []

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

if self.match is not None:
self.match_re = re.compile(self.match)

if recursive:
for root, dirs, files in os.walk(self.path):
for f in files:
Expand All @@ -846,6 +852,7 @@ def __init__(self, path, match=None, recursive=False, required=True,
self.choices.append((full_file, f))
except OSError:
pass

self.widget.choices = self.choices

class SplitDateTimeField(MultiValueField):
Expand Down
13 changes: 13 additions & 0 deletions tests/regressiontests/model_forms_regress/models.py
@@ -1,3 +1,4 @@
import os
from django.db import models
from django import forms

Expand All @@ -12,6 +13,9 @@ def __unicode__(self):
class Meta:
unique_together = (('left', 'middle'), ('middle', 'right'))

class FilePathModel(models.Model):
path = models.FilePathField(path=os.path.dirname(__file__), match=".*\.py$", blank=True)

__test__ = {'API_TESTS': """
When the same field is involved in multiple unique_together constraints, we
need to make sure we don't remove the data for it before doing all the
Expand All @@ -28,5 +32,14 @@ class Meta:
>>> form = TripleForm({'left': '1', 'middle': '3', 'right': '1'})
>>> form.is_valid()
True
# Regression test for #8842: FilePathField(blank=True)
>>> class FPForm(forms.ModelForm):
... class Meta:
... model = FilePathModel
>>> form = FPForm()
>>> [c[1] for c in form['path'].field.choices]
['---------', '__init__.py', 'models.py']
"""}

0 comments on commit a64a61b

Please sign in to comment.