Skip to main content

AutocompleteJsonView

Handle AutocompleteWidget's AJAX requests for data.

Attributes

AttributeTypeDescription
paginate_byint = 20The maximum number of search results to return per page in the autocomplete response.
admin_sitedjango.contrib.admin.sites.AdminSite = nullThe AdminSite instance used to retrieve the ModelAdmin for the target model and validate permissions.

Methods


get()

@classmethod
def get(
request: [HttpRequest](../../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > [JsonResponse](../../../../http/response/jsonresponse.md?sid=django_http_response_jsonresponse)

Return a JsonResponse with search results as defined in serialize_result(), by default: { results: [{id: "123" text: "foo"}], pagination: {more: true} }

Parameters

NameTypeDescription
request[HttpRequest](../../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request object containing search parameters.

Returns

TypeDescription
[JsonResponse](../../../../http/response/jsonresponse.md?sid=django_http_response_jsonresponse)A JSON response containing the serialized search results and a boolean flag indicating if more pages exist.

serialize_result()

@classmethod
def serialize_result(
obj: [Model](../../../../db/models/base/model.md?sid=django_db_models_base_model),
to_field_name: string
) - > dict

Convert the provided model object to a dictionary that is added to the results list.

Parameters

NameTypeDescription
obj[Model](../../../../db/models/base/model.md?sid=django_db_models_base_model)The Django model instance to be serialized into a result dictionary.
to_field_namestringThe name of the model field to use as the unique identifier in the result.

Returns

TypeDescription
dictA dictionary containing the 'id' and 'text' representation of the model instance.

get_paginator()

@classmethod
def get_paginator() - > [Paginator](../../../../core/paginator/paginator.md?sid=django_core_paginator_paginator)

Use the ModelAdmin's paginator.

Returns

TypeDescription
[Paginator](../../../../core/paginator/paginator.md?sid=django_core_paginator_paginator)The paginator instance associated with the target ModelAdmin.

get_queryset()

@classmethod
def get_queryset() - > [QuerySet](../../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)

Return queryset based on ModelAdmin.get_search_results().

Returns

TypeDescription
[QuerySet](../../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)A filtered and potentially distinct QuerySet of model instances matching the search term.

process_request()

@classmethod
def process_request(
request: [HttpRequest](../../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > tuple

Validate request integrity, extract and return request parameters.

Parameters

NameTypeDescription
request[HttpRequest](../../../../http/request/httprequest.md?sid=django_http_request_httprequest)The request object containing the app_label, model_name, and field_name required for validation.

Returns

TypeDescription
tupleA tuple containing the search term, the ModelAdmin instance, the source field, and the target field name.

has_perm()

@classmethod
def has_perm(
request: [HttpRequest](../../../../http/request/httprequest.md?sid=django_http_request_httprequest),
obj: [Model](../../../../db/models/base/model.md?sid=django_db_models_base_model) = null
) - > boolean

Check if user has permission to access the related model.

Parameters

NameTypeDescription
request[HttpRequest](../../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current request object used to verify user authentication and permissions.
obj[Model](../../../../db/models/base/model.md?sid=django_db_models_base_model) = nullAn optional model instance to check permissions against.

Returns

TypeDescription
booleanTrue if the user has view permissions for the related model, otherwise False.