FileField
This class handles file uploads within a form, providing validation for file presence, name length, and content size. It utilizes a clearable file input widget and manages complex states such as contradictory inputs between new uploads and clearing existing files. The class ensures that submitted data conforms to specified constraints before returning an uploaded file object.
Attributes
| Attribute | Type | Description |
|---|---|---|
| widget | Widget class = ClearableFileInput | The default form widget class used to render this field, which handles file uploads and provides a clearable interface. |
| default_error_messages | dict | A dictionary of error message templates used to provide feedback for invalid, missing, empty, or overly long file submissions. |
Constructor
Signature
def FileField(
max_length: int = None,
allow_empty_file: boolean = False,
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| max_length | int = None | The maximum allowed length for the uploaded file's name. |
| allow_empty_file | boolean = False | A flag indicating whether an empty file is considered valid. |
| **kwargs | dict | Additional keyword arguments passed to the parent Field class. |
Methods
to_python()
@classmethod
def to_python(
data: [UploadedFile](../../core/files/uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile)
) - > [UploadedFile](../../core/files/uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile)
Converts the uploaded file data into a file object while validating the filename length and ensuring the file is not empty if required.
Parameters
| Name | Type | Description |
|---|---|---|
| data | [UploadedFile](../../core/files/uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile) | The raw uploaded file object containing name and size attributes |
Returns
| Type | Description |
|---|---|
[UploadedFile](../../core/files/uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile) | The validated file object or None if no data was provided |
clean()
@classmethod
def clean(
data: any,
initial: any = None
) - > [UploadedFile](../../core/files/uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile)
Validates the input data, handling contradictions between new uploads and clearing checkboxes, and falls back to initial data if no new file is provided.
Parameters
| Name | Type | Description |
|---|---|---|
| data | any | The submitted file data or a sentinel value indicating the file should be cleared |
| initial | any = None | The existing file value currently associated with the field |
Returns
| Type | Description |
|---|---|
[UploadedFile](../../core/files/uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile) | The cleaned file data, the initial file value, or False if the field is being cleared |
bound_data()
@classmethod
def bound_data(
_: any,
initial: any
) - > any
Returns the initial data for the field when it is bound to a form.
Parameters
| Name | Type | Description |
|---|---|---|
| _ | any | Unused parameter representing the submitted data |
| initial | any | The initial file value to be returned |
Returns
| Type | Description |
|---|---|
any | The initial file value provided to the field |
has_changed()
@classmethod
def has_changed(
initial: any,
data: any
) - > boolean
Determines if the field value has changed by checking if new data has been submitted and the field is not disabled.
Parameters
| Name | Type | Description |
|---|---|---|
| initial | any | The original file value before form submission |
| data | any | The new file data submitted in the form |
Returns
| Type | Description |
|---|---|
boolean | True if the field is enabled and new data is present, False otherwise |