-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example of another way to create choices on the fly
- Loading branch information
Showing
8 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
25 changes: 25 additions & 0 deletions
25
test_project/ajax_create/templates/admin/ajax_create/creatable/change_form.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
10
test_project/ajax_create/templates/ajax_create/autocomplete.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.
This file contains 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
959ccbe
There was a problem hiding this comment.
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