summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2009-04-08 18:55:06 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2009-04-08 18:55:06 +0000
commit136d8b2854908ab0e43334a5b25d46c324270db6 (patch)
tree3c903aec2a761b12204f6f228ad1565dbb7db218
parent5fc10e92937df953d7d3e40889f14324f1eca785 (diff)
[1.0.X] Fixed #8422: FilePathField now respects required=False. Backport of r10447 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10448 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/forms/fields.py9
-rw-r--r--tests/regressiontests/model_forms_regress/models.py13
2 files changed, 21 insertions, 1 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index d9f4890734..63d06ab0eb 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -828,9 +828,15 @@ class FilePathField(ChoiceField):
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:
@@ -845,6 +851,7 @@ class FilePathField(ChoiceField):
self.choices.append((full_file, f))
except OSError:
pass
+
self.widget.choices = self.choices
class SplitDateTimeField(MultiValueField):
diff --git a/tests/regressiontests/model_forms_regress/models.py b/tests/regressiontests/model_forms_regress/models.py
index dedbed7179..fc587202d6 100644
--- a/tests/regressiontests/model_forms_regress/models.py
+++ b/tests/regressiontests/model_forms_regress/models.py
@@ -1,3 +1,4 @@
+import os
from django.db import models
from django import forms
@@ -12,6 +13,9 @@ class Triple(models.Model):
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
@@ -28,5 +32,14 @@ False
>>> 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']
"""}