Skip to main content

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

AttributeTypeDescription
widgetWidget class = ClearableFileInputThe default form widget class used to render this field, which handles file uploads and provides a clearable interface.
default_error_messagesdictA 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

NameTypeDescription
max_lengthint = NoneThe maximum allowed length for the uploaded file's name.
allow_empty_fileboolean = FalseA flag indicating whether an empty file is considered valid.
**kwargsdictAdditional 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

NameTypeDescription
data[UploadedFile](../../core/files/uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile)The raw uploaded file object containing name and size attributes

Returns

TypeDescription
[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

NameTypeDescription
dataanyThe submitted file data or a sentinel value indicating the file should be cleared
initialany = NoneThe existing file value currently associated with the field

Returns

TypeDescription
[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

NameTypeDescription
_anyUnused parameter representing the submitted data
initialanyThe initial file value to be returned

Returns

TypeDescription
anyThe 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

NameTypeDescription
initialanyThe original file value before form submission
dataanyThe new file data submitted in the form

Returns

TypeDescription
booleanTrue if the field is enabled and new data is present, False otherwise