Skip to main content

FilePathField

This class provides a specialized choice field that allows users to select from files or folders within a specific directory on the file system. It supports filtering entries using regular expressions and can optionally traverse directories recursively to populate its choices. The class automatically manages the mapping between absolute file paths and their display names for use in form widgets.

Attributes

AttributeTypeDescription
pathstringThe absolute directory path from which the field will derive its list of available file or folder choices.
matchstring = nullA regular expression pattern used to filter the files or folders included in the choices.
recursiveboolean = falseA boolean flag that determines whether to include contents of subdirectories when building the list of choices.
allow_filesboolean = trueA boolean flag indicating whether regular files should be included as valid selections in the field.
allow_foldersboolean = falseA boolean flag indicating whether directories should be included as valid selections in the field.
match_rere.PatternThe compiled regular expression object used internally to validate file and folder names against the match pattern.

Constructor

Signature

def FilePathField(
path: string,
match: string = None,
recursive: boolean = False,
allow_files: boolean = True,
allow_folders: boolean = False,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
pathstringThe absolute path to the directory from which to choose files.
matchstring = NoneA regular expression pattern to filter the files or folders.
recursiveboolean = FalseWhether to include subdirectories in the search.
allow_filesboolean = TrueWhether to include files in the choices.
allow_foldersboolean = FalseWhether to include folders in the choices.
**kwargsdictAdditional keyword arguments passed to the parent ChoiceField.

Methods


set_choices()

@classmethod
def set_choices()

Populates the field's choices by scanning the filesystem at the configured path. It filters results based on the match regex, recursion settings, and whether files or folders are allowed, then updates the associated widget.

Returns

TypeDescription
None