python Django:autocomplete-light添加自定义属性以选择和预填充其他字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Django:autocomplete-light添加自定义属性以选择和预填充其他字段相关的知识,希望对你有一定的参考价值。

$(function() {
    'use strict';

    // autocomplete for document number
    $('.js-visitor-document-number').bind('selectChoice', function(e, choice, autocomplete) {
        $('.js-visitor-name').val($(choice).data('name'));
    });
});
from myapp.autocomplete_light_registry import EntranceRegisterAutocomplete


class AssetRequestForm(forms.ModelForm):

    class Meta:
        model = AssetRequest
        fields = (
            'office',
            'delivered_by_name',
            'delivered_by_document_type',
            'delivered_by_document_number',
        )

    def __init__(self, *args, **kwargs):
        super(AssetRequestForm, self).__init__(*args, **kwargs)
        self.fields['delivered_by_name'].widget.attrs['class'] = \
            'js-visitor-name'
        self.fields['delivered_by_document_type'].widget.attrs['class'] = \
            'js-visitor-document-type'
        self.fields['delivered_by_document_number'].widget = \
            autocomplete_light.TextWidget(
                'EntranceRegisterAutocomplete'
            )
        self.fields['delivered_by_document_number'].widget.attrs['class'] = \
            'js-visitor-document-number'

        self.helper = FormHelper()
        self.helper.layout = Layout(
            'office',
            'delivered_by_document_number',
            'delivered_by_document_type',
            'delivered_by_name',
        )
        self.helper.add_input(Submit('submit', u'Guardar'))
{% extends "layout/base.html" %}
{% load crispy_forms_tags static %}

{% block content %}
    {% crispy form %}
{% endblock %}

{% block js %}
    {% include 'autocomplete_light/static.html' %}
{% endblock %}
from django.utils.html import escape

from porter.models import EntranceRegister

import autocomplete_light


class EntranceRegisterAutocomplete(autocomplete_light.AutocompleteModelBase):
    search_fields = [
        '^document_number',
    ]
    autocomplete_js_attributes = {
        'placeholder': u'Digita el número de documento...',
    }

    def choices_for_request(self):
        assert self.choices is not None, 'choices should be a queryset'
        assert self.search_fields, 'autocomplete.search_fields must be set'
        q = self.request.GET.get('q', '')
        if len(q) < 3:
            return EntranceRegister.objects.none()
        exclude = self.request.GET.getlist('exclude')
        conditions = self._choices_for_request_conditions(
            q,
            self.search_fields,
        )
        queryset = self.choices.filter(conditions).exclude(pk__in=exclude)
        queryset = queryset.order_by(
            'document_number',
        ).distinct('document_number')

        return queryset[0:self.limit_choices]

    def choice_label(self, choice):
        return u'{0}'.format(
            choice.document_number,
        )

    def choice_html(self, choice):
        return u'<span data-value="{value}" data-name="{name}">{label}</span>'.format(
            value=escape(self.choice_value(choice)),
            label=escape(self.choice_label(choice)),
            name=escape(choice.name),
        )

autocomplete_light.register(EntranceRegister, EntranceRegisterAutocomplete)

以上是关于python Django:autocomplete-light添加自定义属性以选择和预填充其他字段的主要内容,如果未能解决你的问题,请参考以下文章

Django:django-autocomplete-light 无法正常工作

django-autocomplete-light:“无法加载结果”

django-autocomplete-light 简单用法

如何使用 django-autocomplete-light

使用 django-autocomplete-light 自动完成

如何在内联形式中使用“django-autocomplete-light”