ContentFile
A File-like object that takes just raw content, rather than an actual file.
Attributes
| Attribute | Type | Description |
|---|---|---|
| size | int | The 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
| Name | Type | Description |
|---|---|---|
| content | str or bytes | The raw content to be stored in the file-like object. |
| name | str = None | The 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
| Name | Type | Description |
|---|---|---|
| mode | string = null | The mode in which the file is opened, though it is ignored in this implementation as the stream is already initialized |
Returns
| Type | Description |
|---|---|
[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
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| data | `string | bytes` |
Returns
| Type | Description |
|---|---|
integer | The number of bytes or characters written to the stream |