ModelMultipleChoiceField
A MultipleChoiceField whose choices are a model QuerySet.
Attributes
| Attribute | Type | Description |
|---|---|---|
| widget | Widget class = SelectMultiple | The default form widget class used to render the multiple choice selection interface. |
| hidden_widget | Widget class = MultipleHiddenInput | The widget class used to render the field when the input is hidden. |
| default_error_messages | dict | A dictionary containing localized error messages for invalid lists, invalid choices, and invalid primary key values. |
Constructor
Signature
def ModelMultipleChoiceField(
queryset: [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset),
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| queryset | [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) | A QuerySet of model objects to be used as choices. |
| **kwargs | dict | Additional keyword arguments passed to the parent class constructor. |
Methods
to_python()
@classmethod
def to_python(
value: any
) - > list
Converts the input value into a list of model instances by validating the provided primary keys.
Parameters
| Name | Type | Description |
|---|---|---|
| value | any | The raw input data, typically a list of primary keys, to be converted into model instances. |
Returns
| Type | Description |
|---|---|
list | A list of model objects corresponding to the provided primary keys, or an empty list if no value is provided. |
clean()
@classmethod
def clean(
value: any
) - > [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset)
Validates the input list of values and returns a QuerySet of the corresponding model instances.
Parameters
| Name | Type | Description |
|---|---|---|
| value | any | The submitted data to validate, which should be a list or tuple of primary keys. |
Returns
| Type | Description |
|---|---|
[QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) | A QuerySet containing the model instances matched by the input values. |
prepare_value()
@classmethod
def prepare_value(
value: any
) - > list
Coerces the value into a format suitable for the widget, handling collections of model instances or primary keys.
Parameters
| Name | Type | Description |
|---|---|---|
| value | any | The value to prepare, which can be a single object, a QuerySet, or a list of primary keys. |
Returns
| Type | Description |
|---|---|
list | A list of primary keys extracted from the input model instances or raw values. |
has_changed()
@classmethod
def has_changed(
initial: any,
data: any
) - > boolean
Determines if the submitted data differs from the initial value by comparing sets of primary keys.
Parameters
| Name | Type | Description |
|---|---|---|
| initial | any | The original value of the field, used for comparison. |
| data | any | The new data submitted via the form to check for changes. |
Returns
| Type | Description |
|---|---|
boolean | True if the submitted data represents a different set of objects than the initial data, False otherwise. |