Field¶
The Field class is a dataclass that represents a single field from HTTP form data, containing both metadata and content for form fields and file uploads.
Field Class¶
Constructor¶
@dataclass
class Field:
name: Optional[str] = None
filename: Optional[str] = None
value: Optional[bytes] = b''
content_type: Optional[str] = None
Parameters:
- name: Field name from the form (optional)
- filename: Original filename for file uploads (optional)
- value: Field content as bytes (defaults to empty bytes)
- content_type: MIME type of the field content (optional)
Properties¶
All properties are automatically generated by the @dataclass decorator:
Field Metadata¶
name: The name attribute of the form fieldfilename: Original filename (only present for file uploads)content_type: MIME type of the content
Field Content¶
value: The actual field data as bytes
Magic Methods¶
__repr__()¶
Returns a string representation showing the field name and value length.
Usage Examples¶
Text Field¶
from pyweber.models.field import Field
# Create a text field
text_field = Field(
name="username",
value=b"john_doe",
content_type=None,
filename=None
)
print(text_field.name) # username
print(text_field.value) # b'john_doe'
print(len(text_field.value)) # 8
File Upload Field¶
# Create a file upload field
file_field = Field(
name="avatar",
filename="profile.jpg",
value=b"JPEG binary data...",
content_type="image/jpeg"
)
print(file_field.name) # avatar
print(file_field.filename) # profile.jpg
print(file_field.content_type) # image/jpeg
Default Field¶
# Create field with defaults
default_field = Field()
print(default_field.name) # None
print(default_field.filename) # None
print(default_field.value) # b''
print(default_field.content_type) # None
Field Types¶
Text Fields¶
name: Presentfilename: Nonevalue: Text content as bytescontent_type: None
File Fields¶
name: Present (form field name)filename: Present (original filename)value: File content as bytescontent_type: Present (MIME type)
Integration¶
The Field class is used by:
- FieldStorage for parsing multipart/form-data
- File class for representing uploaded files
- Request class for accessing form data
Common Use Cases¶
- Form Data Processing: Representing individual form fields
- File Upload Handling: Storing file metadata and content
- Data Validation: Checking field names, types, and content
- Content Parsing: Converting between different data formats