MediaType
This class parses and represents a media type string, including its main type, sub-type, and optional parameters. It provides functionality to match media types against each other using wildcard logic and evaluates the specificity and quality of the media type. The class also supports reconstructing the media type into a standardized string format.
Attributes
| Attribute | Type | Description |
|---|---|---|
| params | dict | A dictionary of key-value pairs representing the optional parameters parsed from the media type header line. |
| main_type | string | The primary category of the media type, such as 'text' or 'application', extracted from the portion before the slash. |
| sub_type | string | The specific format of the media type, such as 'html' or 'json', extracted from the portion after the slash. |
| range_params | dict | A copy of the media type parameters excluding the 'q' quality factor, used for matching media ranges. |
| quality | float = 1 | The relative preference weight of the media type, parsed from the 'q' parameter as a float between 0 and 1. |
| specificity | integer | Return a value from 0-3 for how specific the media type is. |
Constructor
Signature
def MediaType(
media_type_raw_line: str
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| media_type_raw_line | str | The raw media type string to be parsed. |
Methods
range_params()
@classmethod
def range_params() - > dict
Extracts all media type parameters excluding the 'q' (quality) parameter to isolate content-specific attributes.
Returns
| Type | Description |
|---|---|
dict | A dictionary of parameters used for matching, with the quality factor removed. |
match()
@classmethod
def match(
other: [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[MediaType](mediatype.md?sid=django_http_request_mediatype), str]
) - > boolean
Determines if this media type is compatible with another media type, accounting for wildcards and parameter subsets.
Parameters
| Name | Type | Description |
|---|---|---|
| other | [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[MediaType](mediatype.md?sid=django_http_request_mediatype), str] | The media type instance or raw string to compare against the current instance. |
Returns
| Type | Description |
|---|---|
boolean | True if the types, sub-types, and parameters are compatible according to content negotiation rules; False otherwise. |
quality()
@classmethod
def quality() - > float
Parses and returns the 'q' parameter value to determine the preference weight of the media type.
Returns
| Type | Description |
|---|---|
float | A float between 0 and 1 representing the quality factor; defaults to 1 if the parameter is missing or invalid. |
specificity()
@classmethod
def specificity() - > integer
Return a value from 0-3 for how specific the media type is.
Returns
| Type | Description |
|---|---|
integer | An integer where 0 is a catch-all wildcard, 1 is a type wildcard, 2 is a specific type/subtype, and 3 includes parameters. |