Last active
September 28, 2015 20:51
-
-
Save cansadadeserfeliz/fd89cafe9f00e1e5c03a to your computer and use it in GitHub Desktop.
Django: autocomplete-light add custom attributes to select and pre-populate other fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% extends "layout/base.html" %} | |
| {% load crispy_forms_tags static %} | |
| {% block content %} | |
| {% crispy form %} | |
| {% endblock %} | |
| {% block js %} | |
| {% include 'autocomplete_light/static.html' %} | |
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function() { | |
| 'use strict'; | |
| // autocomplete for document number | |
| $('.js-visitor-document-number').bind('selectChoice', function(e, choice, autocomplete) { | |
| $('.js-visitor-name').val($(choice).data('name')); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
una consulata.
Se puede crear una clase tipo fields, que incluya una lista como variable para los campos que se van a autocompletar????