Skip to content
Browse files

Added an example of another way to create choices on the fly

  • Loading branch information...
1 parent 1717a57 commit 959ccbefa63117d3337d36086a1a5bdeb8cd74a3 @jpic jpic committed Apr 11, 2013
View
0 test_project/ajax_create/__init__.py
No changes.
View
10 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)
View
16 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)
View
9 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
View
25 test_project/ajax_create/templates/admin/ajax_create/creatable/change_form.html
@@ -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 %}
View
10 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 %}
View
BIN test_project/db.sqlite
Binary file not shown.
View
1 test_project/test_project/settings.py
@@ -177,6 +177,7 @@
'default_template_autocomplete',
'hvad',
'hvad_autocomplete',
+ 'ajax_create',
)
# A sample logging configuration. The only tangible logging

1 comment on commit 959ccbe

@jpic
YourLabs member
jpic commented on 959ccbe Jun 28, 2014

Same example in v2: 070ae50

Please sign in to comment.
Something went wrong with that request. Please try again.