o 3a0@sLddlmZddlmZmZddlmZmZddlm Z Gddde Z dS))apps)FieldDoesNotExistPermissionDenied)Http404 JsonResponse) BaseListViewc@sBeZdZdZdZdZddZddZdd Zd d Z dd d Z dS)AutocompleteJsonViewz3Handle AutocompleteWidget's AJAX requests for data.Ncsd||\|_|_|_||st||_|}t fdd|dDd|d idS)z Return a JsonResponse with search results of the form: { results: [{id: "123" text: "foo"}], pagination: {more: true} } cs$g|]}tt|t|dqS))idtext)strgetattr).0obj to_field_nameI/usr/lib/python3/dist-packages/django/contrib/admin/views/autocomplete.py sz,AutocompleteJsonView.get.. object_listmorepage_obj)results pagination) process_requestterm model_admin source_fieldhas_permr get_querysetrget_context_datarhas_next)selfrequestargskwargscontextrrrget s   zAutocompleteJsonView.getcOs|jj|jg|Ri|S)zUse the ModelAdmin's paginator.)r get_paginatorr#)r"r$r%rrrr(#sz"AutocompleteJsonView.get_paginatorcCsF|j|j}||j}|j|j||j\}}|r!|}|S)z9Return queryset based on ModelAdmin.get_search_results().) rrr#complex_filterrget_limit_choices_toget_search_resultsrdistinct)r"qssearch_use_distinctrrrr's z!AutocompleteJsonView.get_querysetc Csf|jdd}z|jd}|jd}|jd}Wnty'}zt|d}~wwzt||}Wnty?}zt|d}~wwz|j|}Wnt yW}zt|d}~wwz|j j } Wnt ym}zt|d}~wwz|j j| } Wnty}zt|d}~ww| |stdt| jt|j d| jjj} | j| j} | || st|| || fS)a Validate request integrity, extract and return request parameters. Since the subsequent view permission check requires the target model admin, which is determined here, raise PermissionDenied if the requested app, model or field are malformed. Raise Http404 if the target model admin is not configured properly with search_fields. r app_label model_name field_nameNz5%s must have search_fields for the autocomplete_view.)GETr'KeyErrorrr get_model LookupError_meta get_fieldr remote_fieldmodelAttributeError admin_site _registryget_search_fieldsrtype __qualname__r pkattnameto_field_allowed) r"r#rr0r1r2e source_modelr remote_modelrrrrrr0sX       z$AutocompleteJsonView.process_requestcCs|jj||dS)z9Check if user has permission to access the related model.)r)rhas_view_permission)r"r#rrrrrdszAutocompleteJsonView.has_perm)N) __name__ __module__r@__doc__ paginate_byr<r'r(rrrrrrrrs 4rN) django.appsrdjango.core.exceptionsrr django.httprrdjango.views.generic.listrrrrrrs