Created
January 23, 2014 15:06
-
-
Save JuniorLima/8580027 to your computer and use it in GitHub Desktop.
Autocomplete com Yawd Admin
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 forms import SideBarExampleAdminForm, WidgetsExampleAdminForm | |
class WidgetsExampleAdmin(admin.ModelAdmin): | |
form = WidgetsExampleAdminForm | |
admin_site.register(WidgetsExample, WidgetsExampleAdmin) |
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 import forms | |
from django.core.urlresolvers import reverse_lazy | |
from yawdadmin.widgets import AutoCompleteTextInput | |
class WidgetsExampleAdminForm(forms.ModelForm): | |
class Meta: | |
widgets = { | |
'autocomplete': AutoCompleteTextInput(source=\ | |
reverse_lazy('autocomplete-example-view')), | |
} |
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
class WidgetsExample(models.Model): | |
autocomplete = models.CharField(help_text='This widget suggests values based on '\ | |
'other records', | |
max_length=50) |
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 demo_application.views import TypeaheadProfessionsView | |
urlpatterns = patterns('', | |
#register the yawd-admin URLs | |
url(r'^autocomplete-example/', TypeaheadProfessionsView.as_view(), name='autocomplete-example-view') | |
) |
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
import json | |
from django.views.generic import View | |
from django.http.response import HttpResponse | |
from django.core.exceptions import PermissionDenied | |
from models import WidgetsExample | |
# Create your views here. | |
class TypeaheadProfessionsView(View): | |
def get(self, request, *args, **kwargs): | |
if not request.is_ajax(): | |
raise PermissionDenied | |
query = request.GET.get('query', None) | |
results = [] | |
for el in WidgetsExample.objects.values_list('autocomplete', flat=True).distinct(): | |
if el and (not query or el.find(query.decode('utf-8')) != -1): | |
results.append(el) | |
return HttpResponse(json.dumps({'results': results})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment