mirror of
https://github.com/Hopiu/django-select2.git
synced 2026-04-25 23:44:47 +00:00
The changes in this code crashes Python 2.7.1 (OSX build)
This commit is contained in:
parent
8579993107
commit
703d540689
13 changed files with 135 additions and 40 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -2,3 +2,5 @@
|
|||
testapp/tt.py
|
||||
|
||||
*.pyc
|
||||
|
||||
testapp/tt2.py
|
||||
|
|
|
|||
|
|
@ -12,3 +12,10 @@ External dependencies
|
|||
|
||||
* Django - This is obvious.
|
||||
* jQuery - This is not included in the package since it is expected that in most scenarios this would already be available.
|
||||
|
||||
Changelog Summary
|
||||
=================
|
||||
|
||||
v2.0
|
||||
----
|
||||
Mostly major bug fixes in code and design. The changes were many, raising the possibility of backward incompatibilty. However, the backward incompatibilty would be subtle.
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
__version__ = "1.1"
|
||||
__version__ = "2.0"
|
||||
|
||||
from django.conf import settings
|
||||
if settings.configured:
|
||||
from .widgets import Select2Widget, Select2MultipleWidget, HeavySelect2Widget, HeavySelect2MultipleWidget, AutoHeavySelect2Widget
|
||||
from .fields import Select2ChoiceField, Select2MultipleChoiceField, \
|
||||
HeavySelect2ChoiceField, HeavySelect2MultipleChoiceField, \
|
||||
ModelSelect2Field, AutoSelect2Field, AutoModelSelect2Field
|
||||
ModelSelect2Field, AutoSelect2Field, AutoModelSelect2Field #, ModelMultipleSelect2Field
|
||||
from .views import Select2View, NO_ERR_RESP
|
||||
|
|
|
|||
|
|
@ -125,7 +125,9 @@ class QuerysetChoiceMixin(ChoiceMixin):
|
|||
|
||||
choices = property(_get_choices, ChoiceMixin._set_choices)
|
||||
|
||||
class ModelChoiceField(forms.ModelChoiceField):
|
||||
class ModelChoiceFieldMixin(object):
|
||||
#class ModelChoiceField(forms.ModelChoiceField):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
queryset = kwargs.pop('queryset', None)
|
||||
empty_label = kwargs.pop('empty_label', u"---------")
|
||||
|
|
@ -151,7 +153,13 @@ class ModelChoiceField(forms.ModelChoiceField):
|
|||
if hasattr(self, '_queryset'):
|
||||
return self._queryset
|
||||
|
||||
queryset = property(_get_queryset, forms.ModelChoiceField._set_queryset)
|
||||
# queryset = property(_get_queryset, forms.ModelChoiceField._set_queryset)
|
||||
|
||||
class ModelChoiceField(ModelChoiceFieldMixin, forms.ModelChoiceField):
|
||||
queryset = property(ModelChoiceFieldMixin._get_queryset, forms.ModelChoiceField._set_queryset)
|
||||
|
||||
#class ModelMultipleChoiceField(ModelChoiceFieldMixin, forms.ModelMultipleChoiceField):
|
||||
# queryset = property(ModelChoiceFieldMixin._get_queryset, forms.ModelMultipleChoiceField._set_queryset)
|
||||
|
||||
class Select2ChoiceField(forms.ChoiceField):
|
||||
widget = Select2Widget
|
||||
|
|
@ -215,3 +223,6 @@ class ModelSelect2Field(ModelChoiceField) :
|
|||
"Light Model Select2 field"
|
||||
widget = Select2Widget
|
||||
|
||||
#class ModelMultipleSelect2Field(ModelMultipleChoiceField) :
|
||||
# "Light multiple-value Model Select2 field"
|
||||
# widget = Select2MultipleWidget
|
||||
|
|
|
|||
|
|
@ -10,6 +10,25 @@ def render_js_script(inner_code):
|
|||
});
|
||||
</script>""" % inner_code
|
||||
|
||||
class JSVar(unicode):
|
||||
"Denotes a JS variable name, so it must not be quoted while rendering."
|
||||
pass
|
||||
|
||||
class JSFunction(JSVar):
|
||||
"""
|
||||
Flags that the string is the name of a JS function. Used by Select2Mixin.render_options()
|
||||
to make sure that this string is not quoted like other strings.
|
||||
"""
|
||||
pass
|
||||
|
||||
class JSFunctionInContext(JSVar):
|
||||
"""
|
||||
Like JSFunction, this too flags the string as JS function, but with a special requirement.
|
||||
The JS function needs to be invoked in the context of the current Select2 Html DOM,
|
||||
such that 'this' inside the function refers to the source Select2 DOM.
|
||||
"""
|
||||
pass
|
||||
|
||||
### Auto view helper utils ###
|
||||
|
||||
import re
|
||||
|
|
|
|||
|
|
@ -5,26 +5,7 @@ from django import forms
|
|||
from django.utils.safestring import mark_safe
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from .util import render_js_script, convert_to_js_string_arr
|
||||
|
||||
class JSVar(unicode):
|
||||
"Denotes a JS variable name, so it must not be quoted while rendering."
|
||||
pass
|
||||
|
||||
class JSFunction(JSVar):
|
||||
"""
|
||||
Flags that the string is the name of a JS function. Used by Select2Mixin.render_options()
|
||||
to make sure that this string is not quoted like other strings.
|
||||
"""
|
||||
pass
|
||||
|
||||
class JSFunctionInContext(JSVar):
|
||||
"""
|
||||
Like JSFunction, this too flags the string as JS function, but with a special requirement.
|
||||
The JS function needs to be invoked in the context of the current Select2 Html DOM,
|
||||
such that 'this' inside the function refers to the source Select2 DOM.
|
||||
"""
|
||||
pass
|
||||
from .util import render_js_script, convert_to_js_string_arr, JSVar, JSFunction, JSFunctionInContext
|
||||
|
||||
class Select2Mixin(object):
|
||||
# For details on these options refer: http://ivaynberg.github.com/select2/#documentation
|
||||
|
|
|
|||
BIN
testapp/test.db
BIN
testapp/test.db
Binary file not shown.
|
|
@ -1,5 +1,6 @@
|
|||
{% load url from future %}
|
||||
<h1>Manual Tests</h1>
|
||||
<ul>
|
||||
<li><a href="{% url 'test_auto_model_field' %}">Test single selection model fields</a></li>
|
||||
<li><a href="{% url 'test_single_value_model_field' %}">Test single selection model fields</a></li>
|
||||
<li><a href="{% url 'test_multi_values_model_field' %}">Test multi selection model fields</a></li>
|
||||
</ul>
|
||||
|
|
@ -1,23 +1,61 @@
|
|||
[
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "testmain.classroom",
|
||||
"fields": {
|
||||
"number": "100"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "testmain.classroom",
|
||||
"fields": {
|
||||
"number": "200"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "testmain.classroom",
|
||||
"fields": {
|
||||
"number": "203"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 4,
|
||||
"model": "testmain.classroom",
|
||||
"fields": {
|
||||
"number": "203A"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 5,
|
||||
"model": "testmain.classroom",
|
||||
"fields": {
|
||||
"number": "500"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "testmain.dept",
|
||||
"fields": {
|
||||
"name": "Chemistry"
|
||||
"name": "Chemistry",
|
||||
"allotted_rooms": [1, 2, 3]
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "testmain.dept",
|
||||
"fields": {
|
||||
"name": "Biology"
|
||||
"name": "Biology",
|
||||
"allotted_rooms": [3, 4]
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "testmain.dept",
|
||||
"fields": {
|
||||
"name": "Physics"
|
||||
"name": "Physics",
|
||||
"allotted_rooms": [1, 2, 5]
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from django import forms
|
|||
|
||||
from django_select2 import *
|
||||
|
||||
from .models import Employee, Dept
|
||||
from .models import Employee, Dept, ClassRoom
|
||||
|
||||
class EmployeeChoices(AutoModelSelect2Field):
|
||||
queryset = Employee.objects
|
||||
|
|
@ -14,4 +14,9 @@ class EmployeeForm(forms.ModelForm):
|
|||
|
||||
class Meta:
|
||||
model = Employee
|
||||
|
||||
|
||||
#class DeptForm(forms.ModelForm):
|
||||
# allotted_rooms = ModelMultipleSelect2Field(queryset=ClassRoom.objects)
|
||||
|
||||
# class Meta:
|
||||
# model = Dept
|
||||
|
|
@ -1,7 +1,14 @@
|
|||
from django.db import models
|
||||
|
||||
class ClassRoom(models.Model):
|
||||
number = models.CharField(max_length=4)
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.number)
|
||||
|
||||
class Dept(models.Model):
|
||||
name = models.CharField(max_length=10)
|
||||
allotted_rooms = models.ManyToManyField(ClassRoom)
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.name)
|
||||
|
|
@ -14,3 +21,4 @@ class Employee(models.Model):
|
|||
|
||||
def __unicode__(self):
|
||||
return unicode(self.name)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
from django.conf.urls.defaults import *
|
||||
|
||||
urlpatterns = patterns("",
|
||||
url(r'auto/model/field/$', 'testmain.views.test_auto_model_field', name='test_auto_model_field'),
|
||||
url(r'auto/model/field/([0-9]+)/$', 'testmain.views.test_auto_model_field1', name='test_auto_model_field2'),
|
||||
url(r'single/model/field/$', 'testmain.views.test_single_value_model_field', name='test_single_value_model_field'),
|
||||
url(r'single/model/field/([0-9]+)/$', 'testmain.views.test_single_value_model_field1', name='test_single_value_model_field1'),
|
||||
|
||||
url(r'multi/model/field/$', 'testmain.views.test_multi_values_model_field', name='test_multi_values_model_field'),
|
||||
url(r'multi/model/field/([0-9]+)/$', 'testmain.views.test_multi_values_model_field1', name='test_multi_values_model_field1'),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render_to_response
|
||||
from django.shortcuts import render_to_response, get_object_or_404
|
||||
from django.template import RequestContext
|
||||
|
||||
from .forms import EmployeeForm
|
||||
from .models import Employee
|
||||
from .forms import EmployeeForm #, DeptForm
|
||||
from .models import Employee, Dept
|
||||
|
||||
def test_auto_model_field(request):
|
||||
def test_single_value_model_field(request):
|
||||
return render_to_response('list.html', RequestContext(request, {
|
||||
'title': 'Employees',
|
||||
'href': 'test_auto_model_field2',
|
||||
'href': 'test_single_value_model_field1',
|
||||
'object_list': Employee.objects.all()
|
||||
}))
|
||||
|
||||
def test_auto_model_field1(request, id):
|
||||
emp = Employee.objects.get(id=id)
|
||||
def test_single_value_model_field1(request, id):
|
||||
emp = get_object_or_404(Employee, pk=id)
|
||||
if request.POST:
|
||||
form = EmployeeForm(data=request.POST, instance=emp)
|
||||
if form.is_valid():
|
||||
|
|
@ -22,4 +22,24 @@ def test_auto_model_field1(request, id):
|
|||
return HttpResponseRedirect(reverse('home'))
|
||||
else:
|
||||
form = EmployeeForm(instance=emp)
|
||||
return render_to_response('form.html', RequestContext(request, {'form': form}))
|
||||
return render_to_response('form.html', RequestContext(request, {'form': form}))
|
||||
|
||||
|
||||
def test_multi_values_model_field(request):
|
||||
return render_to_response('list.html', RequestContext(request, {
|
||||
'title': 'Departments',
|
||||
'href': 'test_multi_values_model_field1',
|
||||
'object_list': Dept.objects.all()
|
||||
}))
|
||||
|
||||
def test_multi_values_model_field1(request, id):
|
||||
dept = get_object_or_404(Dept, pk=id)
|
||||
if request.POST:
|
||||
form = DeptForm(data=request.POST, instance=dept)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return HttpResponseRedirect(reverse('home'))
|
||||
else:
|
||||
form = DeptForm(instance=dept)
|
||||
return render_to_response('form.html', RequestContext(request, {'form': form}))
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue