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
| Attribute | Type | Description |
|---|---|---|
| path | string | The absolute directory path from which the field will derive its list of available file or folder choices. |
| match | string = null | A regular expression pattern used to filter the files or folders included in the choices. |
| recursive | boolean = false | A boolean flag that determines whether to include contents of subdirectories when building the list of choices. |
| allow_files | boolean = true | A boolean flag indicating whether regular files should be included as valid selections in the field. |
| allow_folders | boolean = false | A boolean flag indicating whether directories should be included as valid selections in the field. |
| match_re | re.Pattern | The 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
| Name | Type | Description |
|---|---|---|
| path | string | The absolute path to the directory from which to choose files. |
| match | string = None | A regular expression pattern to filter the files or folders. |
| recursive | boolean = False | Whether to include subdirectories in the search. |
| allow_files | boolean = True | Whether to include files in the choices. |
| allow_folders | boolean = False | Whether to include folders in the choices. |
| **kwargs | dict | Additional 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
| Type | Description |
|---|---|
| None |