Resolver Functions¶
Core functions for resolving identifiers to database objects.
resolve_object¶
The central resolver function used by all mixins.
from django_display_ids import resolve_object
# Auto-detects prefix, uuid_field, slug_field, and strategies from the model
invoice = resolve_object(Invoice, "inv_2aUyqjCzEIiEcYMKj7TZtw")
Parameters¶
modelThe Django model class to query.
valueThe identifier string to resolve.
strategiesTuple of strategy names to try, in order. Defaults to
("display_id", "uuid", "slug").prefixExpected display ID prefix. When
None(the default), auto-detected from the model’sdisplay_id_prefixattribute (set byDisplayIDModel).uuid_fieldName of the UUID field on the model. When
None(the default), auto-detected from the model’suuid_fieldattribute (set byDisplayIDModel), then theDISPLAY_IDS["UUID_FIELD"]setting, then"id".slug_fieldName of the slug field on the model. When
None(the default), auto-detected from the model’sslug_fieldattribute (set byDisplayIDModel), then theDISPLAY_IDS["SLUG_FIELD"]setting, then"slug".querysetOptional pre-filtered queryset. If not provided, uses
model.objects.all().
Return Value¶
Returns the matched model instance.
Exceptions¶
InvalidIdentifierError— No strategy could parse the identifierUnknownPrefixError— Display ID prefix doesn’t match expectedObjectNotFoundError— No database record matchesAmbiguousIdentifierError— Multiple records match (slug lookup)
get_model_for_prefix¶
Look up a model class by its registered display ID prefix.
from django_display_ids import get_model_for_prefix
model_class = get_model_for_prefix("inv")
# -> <class 'myapp.models.Invoice'>
Returns None if no model is registered with that prefix.
Prefix Registration¶
Prefixes are automatically registered when a model class with
DisplayIDModel is defined. You don’t need to manually register prefixes.