Skip to content

Commit

Permalink
Added an example of another way to create choices on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
jpic committed Apr 11, 2013
1 parent 1717a57 commit 959ccbe
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 0 deletions.
Empty file.
10 changes: 10 additions & 0 deletions test_project/ajax_create/admin.py
@@ -0,0 +1,10 @@
from django.contrib import admin

import autocomplete_light

from models import Creatable


class CreatableAdmin(admin.ModelAdmin):
form = autocomplete_light.modelform_factory(Creatable)
admin.site.register(Creatable, CreatableAdmin)
16 changes: 16 additions & 0 deletions test_project/ajax_create/autocomplete_light_registry.py
@@ -0,0 +1,16 @@
from django import http

import autocomplete_light

from models import Creatable


class AutocompleteCreatable(autocomplete_light.AutocompleteModelTemplate):
autocomplete_template = 'ajax_create/autocomplete.html'

def post(self, request, *args, **kwargs):
choice = Creatable.objects.create(name=request.POST['createChoice'])
return http.HttpResponse(self.choice_html(choice))


autocomplete_light.register(Creatable, AutocompleteCreatable)
9 changes: 9 additions & 0 deletions test_project/ajax_create/models.py
@@ -0,0 +1,9 @@
from django.db import models


class Creatable(models.Model):
name = models.CharField(max_length=100)
related = models.ManyToManyField('self', blank=True)

def __unicode__(self):
return self.name
@@ -0,0 +1,25 @@
{% extends 'admin/change_form.html' %}

{% block extrahead %}
{{ block.super }}
<script type="text/javascript">
$(document).ready(function() {
$('.autocomplete-light-widget').on(
'click', '.yourlabs-autocomplete [data-create-choice]', function() {
var widget = $(this).parents('.autocomplete-light-widget[data-bootstrap]');
var url = widget.data('autocompleteUrl');

$.ajax(url, {
async: false,
type: 'POST',
data: $(this).data(),
success: function(data, textStatus, jqXHR) {
var widgetInstance = widget.yourlabsWidget();
widgetInstance.selectChoice($(data.replace(/[\n\r]/g, '')));
}
});
}
);
})
</script>
{% endblock %}
10 changes: 10 additions & 0 deletions test_project/ajax_create/templates/ajax_create/autocomplete.html
@@ -0,0 +1,10 @@
{% load autocomplete_light_tags %}
{% load i18n %}

{% for choice in choices %}
{{ choice|autocomplete_light_choice_html:autocomplete }}
{% empty %}
<span class="div" data-create-choice="{{ request.GET.q }}">
{% blocktrans with choice=request.GET.q %}No result found for {{ choice }}, click here to create it.{% endblocktrans %}
</span>
{% endfor %}
Binary file modified test_project/db.sqlite
Binary file not shown.
1 change: 1 addition & 0 deletions test_project/test_project/settings.py
Expand Up @@ -177,6 +177,7 @@
'default_template_autocomplete',
'hvad',
'hvad_autocomplete',
'ajax_create',
)

# A sample logging configuration. The only tangible logging
Expand Down

1 comment on commit 959ccbe

@jpic
Copy link
Member Author

@jpic jpic commented on 959ccbe Jun 28, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same example in v2: 070ae50

Please sign in to comment.