Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#589 closed enhancement (fixed)

A patch that adds FilePathField

Reported by: jay@… Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I coded a patch that implements the FilePathField that was discussed at and

The interface works like this:

fieldName = meta.FilePathField(path='/path/to/files', match='re_string', recursive=True)

path is the location of the files you want to choose
match is a string that is put into re.compile(match) so that only filenames that match that re are included in the list
recursive is a boolean which when set to True allows all the files in subdirectories of path to be included in the list

path is the only required parameter.

I am not sure of the correct way to submit patches so you can download it at
and I have pasted it inline below.

Index: trunk/django/core/meta/
--- trunk/django/core/meta/    (revision 770)
+++ trunk/django/core/meta/    (working copy)
@@ -427,6 +427,43 @@
         f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
         return os.path.normpath(f)

+class FilePathField(Field):
+    def __init__(self, verbose_name=None, name=None, path='', match='', recursive=False, **kwargs):
+        self.path = path
+        self.match = match
+        self.recursive = recursive
+        Field.__init__(self, verbose_name, name, **kwargs)
+    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False):
+       if self.match != '':
+           import re
+           p = re.compile(self.match)
+        files = os.listdir(self.path)
+        theFileList = []
+        if self.recursive:
+            for root, dirs, files in os.walk(self.path):
+                for f in files:
+                   if self.match != '':
+                       m = p.match(f)
+                       if m:
+                           theFileList.append(tuple([ os.path.join(self.path, f), f]))
+                   else:
+                       theFileList.append(tuple([ os.path.join(self.path, f), f]))
+        else:
+            for f in files:
+                if os.path.isfile(os.path.join(self.path,f)):
+                   if self.match != '':
+                       m = p.match(f)
+                       if m:
+                               theFileList.append(tuple([ os.path.join(self.path,f), f]))
+                   else:
+                       theFileList.append(tuple([ os.path.join(self.path,f), f]))
+        self.choices = tuple(theFileList)
+        return Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel)
+    def get_manipulator_field_objs(self):
+        return [formfields.TextField]
 class FloatField(Field):
     empty_strings_allowed = False
     def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs):

Attachments (1)

FilePathField.diff (1.9 KB) - added by jay@… 13 years ago.
FilePathField patch

Download all attachments as: .zip

Change History (3)

Changed 13 years ago by jay@…

Attachment: FilePathField.diff added

FilePathField patch

comment:1 Changed 13 years ago by Adrian Holovaty

Status: newassigned

comment:2 Changed 13 years ago by Adrian Holovaty

Resolution: fixed
Status: assignedclosed

(In [846]) Fixed #589 -- Added FilePathField. It's available as an ORM field and as a standalone field in django.core.formfields. Thanks, jay@…

Note: See TracTickets for help on using tickets.
Back to Top