This class serves as the base for all database models, providing the core functionality for mapping Python objects to database tables. It handles instance initialization, state management, and lifecycle operations such as saving, deleting, and refreshing data from the database. The class also includes comprehensive validation logic for fields, unique constraints, and model-level consistency checks.
Attributes
| Attribute | Type | Description |
|---|
| pk | property | Proxy property that provides access to the value of the model's primary key field. |
Constructor
Signature
def Model(
*args: tuple,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|
| *args | tuple | Positional arguments used to initialize model fields in the order they are defined. |
| **kwargs | dict | Keyword arguments representing field names and their corresponding values for initialization. |
Methods
from_db()
@classmethod
def from_db(
db: str,
field_names: list,
values: list,
fetch_mode: str = null
) - > [Model](model.md?sid=django_db_models_base_model)
Creates a model instance from database values, bypassing the standard constructor to handle deferred fields and database state.
Parameters
| Name | Type | Description |
|---|
| db | str | The alias of the database the instance was loaded from. |
| field_names | list | The names of the fields being loaded from the database. |
| values | list | The raw values for the fields, in the order they appear in the model's concrete fields. |
| fetch_mode | str = null | The mode used to fetch the data, such as FETCH_ONE. |
Returns
| Type | Description |
|---|
[Model](model.md?sid=django_db_models_base_model) | A new instance of the model populated with data from the database. |
get_deferred_fields()
@classmethod
def get_deferred_fields() - > set
Return a set containing names of deferred fields for this instance.
Returns
| Type | Description |
|---|
set | A set of attribute names for fields that have not yet been loaded from the database. |
refresh_from_db()
@classmethod
def refresh_from_db(
using: str = null,
fields: iterable = null,
from_queryset: [QuerySet](../query/queryset.md?sid=django_db_models_query_queryset) = null
)
Reload field values from the database. By default, the reloading happens from the database this instance was loaded from.
Parameters
| Name | Type | Description |
|---|
| using | str = null | The database alias to use for the reload; overrides the default router. |
| fields | iterable = null | An iterable of field attribute names to specifically reload. |
| from_queryset | [QuerySet](../query/queryset.md?sid=django_db_models_query_queryset) = null | A custom queryset to use for fetching the updated instance data. |
arefresh_from_db()
@classmethod
def arefresh_from_db(
using: str = null,
fields: iterable = null,
from_queryset: [QuerySet](../query/queryset.md?sid=django_db_models_query_queryset) = null
) - > Coroutine
Asynchronously reload field values from the database by wrapping refresh_from_db.
Parameters
| Name | Type | Description |
|---|
| using | str = null | The database alias to use for the reload. |
| fields | iterable = null | Specific field names to reload. |
| from_queryset | [QuerySet](../query/queryset.md?sid=django_db_models_query_queryset) = null | A custom queryset to use for the fetch. |
Returns
| Type | Description |
|---|
Coroutine | A coroutine that completes the database refresh. |
serializable_value()
@classmethod
def serializable_value(
field_name: str
) - > any
Return the value of the field name for this instance. If the field is a foreign key, return the id value instead of the object.
Parameters
| Name | Type | Description |
|---|
| field_name | str | The name of the field whose serializable value is requested. |
Returns
| Type | Description |
|---|
any | The raw field value or the primary key of a related object, suitable for serialization. |
save()
@classmethod
def save(
force_insert: bool = false,
force_update: bool = false,
using: str = null,
update_fields: iterable = null
)
Save the current instance. Override this in a subclass if you want to control the saving process.
Parameters
| Name | Type | Description |
|---|
| force_insert | bool = false | If True, forces the save to perform an SQL INSERT. |
| force_update | bool = false | If True, forces the save to perform an SQL UPDATE. |
| using | str = null | The database alias to use for the save operation. |
| update_fields | iterable = null | An iterable of field names to update; if provided, only these fields are saved. |
asave()
@classmethod
def asave(
force_insert: bool = false,
force_update: bool = false,
using: str = null,
update_fields: iterable = null
) - > Coroutine
Asynchronously save the current instance to the database.
Parameters
| Name | Type | Description |
|---|
| force_insert | bool = false | Forces an SQL INSERT. |
| force_update | bool = false | Forces an SQL UPDATE. |
| using | str = null | The database alias to use. |
| update_fields | iterable = null | Specific fields to update. |
Returns
| Type | Description |
|---|
Coroutine | A coroutine that completes the save operation. |
delete()
@classmethod
def delete(
using: str = null,
keep_parents: bool = false
) - > tuple
Removes the current instance from the database and handles related object cleanup.
Parameters
| Name | Type | Description |
|---|
| using | str = null | The database alias to use for the deletion. |
| keep_parents | bool = false | If True, prevents the deletion of parent model records in multi-table inheritance. |
Returns
| Type | Description |
|---|
tuple | A tuple containing the total number of objects deleted and a dictionary with the count per object type. |
adelete()
@classmethod
def adelete(
using: str = null,
keep_parents: bool = false
) - > Coroutine
Asynchronously removes the current instance from the database.
Parameters
| Name | Type | Description |
|---|
| using | str = null | The database alias to use. |
| keep_parents | bool = false | Whether to preserve parent model records. |
Returns
| Type | Description |
|---|
Coroutine | A coroutine that completes the deletion. |
clean()
Hook for doing any extra model-wide validation after clean() has been called on every field by self.clean_fields.
validate_unique()
@classmethod
def validate_unique(
exclude: iterable = null
)
Check unique constraints on the model and raise ValidationError if any failed.
Parameters
| Name | Type | Description |
|---|
| exclude | iterable = null | A collection of field names to exclude from uniqueness checks. |
full_clean()
@classmethod
def full_clean(
exclude: iterable = null,
validate_unique: bool = true,
validate_constraints: bool = true
)
Call clean_fields(), clean(), validate_unique(), and validate_constraints() on the model. Raise a ValidationError for any errors that occur.
Parameters
| Name | Type | Description |
|---|
| exclude | iterable = null | Field names to skip during the cleaning process. |
| validate_unique | bool = true | Whether to perform uniqueness validation. |
| validate_constraints | bool = true | Whether to perform model constraint validation. |
clean_fields()
@classmethod
def clean_fields(
exclude: iterable = null
)
Clean all fields and raise a ValidationError containing a dict of all validation errors if any occur.
Parameters
| Name | Type | Description |
|---|
| exclude | iterable = null | Field names to exclude from individual field validation. |
check()
@classmethod
def check(
kwargs: dict
) - > list
Runs system checks on the model to identify configuration errors, field clashes, or database incompatibilities.
Parameters
| Name | Type | Description |
|---|
| kwargs | dict | Additional arguments for the check framework, such as 'databases'. |
Returns
| Type | Description |
|---|
list | A list of check error and warning objects. |