diff options
author | Federico Bond <federicobond@gmail.com> | 2018-08-19 20:21:57 -0300 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2018-08-20 17:58:00 -0400 |
commit | a0ca4b5694f43c63ea13ba6908eff2bd53ee7ebb (patch) | |
tree | 334ed68c11729a871dbb04e2ceb78b13a83be708 /django/forms/fields.py | |
parent | 371ece2f0682e51f2f796854d3e091827a7cea63 (diff) | |
download | django-a0ca4b5694f43c63ea13ba6908eff2bd53ee7ebb.tar.gz |
Fixed #29689 -- Improved performance of FileSystemStorage.listdir() and FilePathField with os.scandir().
Diffstat (limited to 'django/forms/fields.py')
-rw-r--r-- | django/forms/fields.py | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index 0f1a352cdc..9cc93a46e1 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -5,6 +5,7 @@ Field classes. import copy import datetime import math +import operator import os import re import uuid @@ -1104,17 +1105,16 @@ class FilePathField(ChoiceField): f = os.path.join(root, f) self.choices.append((f, f.replace(path, "", 1))) else: - try: - for f in sorted(os.listdir(self.path)): - if f == '__pycache__': - continue - full_file = os.path.join(self.path, f) - if (((self.allow_files and os.path.isfile(full_file)) or - (self.allow_folders and os.path.isdir(full_file))) and - (self.match is None or self.match_re.search(f))): - self.choices.append((full_file, f)) - except OSError: - pass + choices = [] + for f in os.scandir(self.path): + if f.name == '__pycache__': + continue + if (((self.allow_files and f.is_file()) or + (self.allow_folders and f.is_dir())) and + (self.match is None or self.match_re.search(f.name))): + choices.append((f.path, f.name)) + choices.sort(key=operator.itemgetter(1)) + self.choices.extend(choices) self.widget.choices = self.choices |