Skip to main content

ContentFile

A File-like object that takes just raw content, rather than an actual file.

Attributes

AttributeTypeDescription
sizeintThe total length of the raw content in bytes or characters, used to track the file size and cleared upon write operations.

Constructor

Signature

def ContentFile(
content: str or bytes,
name: str = None
) - > null

Parameters

NameTypeDescription
contentstr or bytesThe raw content to be stored in the file-like object.
namestr = NoneThe name of the file.

Methods


open()

@classmethod
def open(
mode: string = null
) - > [ContentFile](contentfile.md?sid=django_core_files_base_contentfile)

Resets the internal stream pointer to the beginning and returns the file-like object for reading or writing.

Parameters

NameTypeDescription
modestring = nullThe mode in which the file is opened, though it is ignored in this implementation as the stream is already initialized

Returns

TypeDescription
[ContentFile](contentfile.md?sid=django_core_files_base_contentfile)The instance itself, with the stream position reset to zero

close()

@classmethod
def close() - > null

Performs a no-op to satisfy the file-like interface without closing the underlying memory stream.

Returns

TypeDescription
null

write()

@classmethod
def write(
data: string|bytes
) - > integer

Writes data to the underlying stream and clears any cached file size to ensure consistency.

Parameters

NameTypeDescription
data`stringbytes`

Returns

TypeDescription
integerThe number of bytes or characters written to the stream