django-clever-selects
=====================
This library is inspired by **django-chained-selectbox** from *s-block*
(https://github.com/s-block/django-chained-selectbox).
It serves chained select box widget for Django framework using AJAX requests for chaining select boxes together.
The values change depending on the parent value.
Previous mentioned library was intended for use in Django admin only. The new library has frontend functionality,
improved existing instance data initialization and new ``ChainedModelChoiceField``. It also uses custom TestClient which
is able pass ``request.user`` variable into AJAX view if user is logged in. It is very useful if you need to filter result queryset by
user permissions for example.
Tested on Django 1.4.5.
Requirements
------------
- Django
- jQuery
Installation
------------
1. Install python library using pip: pip install django-clever-selects
2. Add ``clever_selects`` to ``INSTALLED_APPS`` in your Django settings file
3. Add ``clever_selects_extras`` to your ``{% load %}`` statement and put ``{% clever_selects_js_import %}`` tag before closing ``</body>`` element. It is important to load clever-selects.js file after body content, so do not put it in the <head></head>!
Usage
-----
Forms
'''''
Form must inherit from ``ChainedChoicesMixin`` (or from ``ChainedChoicesForm`` / ``ChainedChoicesModelForm``, depends on your needs)
which loads the options when there is already an instance or initial data::
from clever_selects.form_fields import ChainedChoiceField
from clever_selects.forms import ChainedChoicesForm
class SimpleChainForm(ChainedChoicesForm):
first_field = ChoiceField(choices=(('', '------------'), (1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5'), ))
second_field = ChainedChoiceField(parent_field='first_field', ajax_url=reverse_lazy('ajax_chained_view'))
class MultipleChainForm(ChainedChoicesForm):
first_field = ChoiceField(choices=(('', '------------'), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), ))
second_field = ChainedChoiceField(parent_field='first_field', ajax_url=reverse_lazy('ajax_chained_view'))
third_field = ChainedChoiceField(parent_field='second_field', ajax_url=reverse_lazy('ajax_chained_view'))
fourth_field = ChainedChoiceField(parent_field='third_field', ajax_url=reverse_lazy('ajax_chained_view'))
fifth_field = ChainedChoiceField(parent_field='fourth_field', ajax_url=reverse_lazy('ajax_chained_view'))
class ModelChainForm(ChainedChoicesModelForm):
brand = forms.ModelChoiceField(queryset=CarBrand.objects.all(), required=True,
empty_label=_('Select a car brand'))
model = ChainedModelChoiceField(parent_field='brand', ajax_url=reverse_lazy('ajax_chained_models'),
empty_label=_('Select a car model'), model=BrandModel, required=True)
engine = forms.ChoiceField(choices=([('', _('All engine types'))] + Car.ENGINES), required=False)
color = ChainedChoiceField(parent_field='model', ajax_url=reverse_lazy('ajax_chained_colors'),
empty_label=_('Select a car model'), required=False)
class Meta:
model = Car
Notice that ajax URLs could differ of each field for different purposes. See example project for more use cases.
In order to pre-populate child fields, the form can need to have access to the current user. This can be done by passing
the user to the kwargs of the form's __init__() method in the form's view. The ChainedSelectFormViewMixin takes care
of this for you.::
class CreateCarView(ChainedSelectFormViewMixin, CreateView)
template_name = "create_car.html"
form_class = ModelChainForm
model = Car
Views
'''''
Ajax call is made whenever the parent field is changed. You must set up the ajax URL to return json list of lists::
class AjaxChainedView(BaseDetailView):
"""
View to handle the ajax request for the field options.
"""
def get(self, request, *args, **kwargs):
field = request.GET.get('field')
parent_value = request.GET.get("parent_value")
vals_list = []
for x in range(1, 6):
vals_list.append(x*int(parent_value))
choices = tuple(zip(vals_list, vals_list))
response = HttpResponse(
json.dumps(choices, cls=DjangoJSONEncoder),
mimetype='application/javascript'
)
add_never_cache_headers(response)
return response
Or you can use ``ChainedSelectChoicesView`` class helper like this::
class AjaxChainedView(ChainedSelectChoicesView):
def get_choices(self):
vals_list = []
for x in range(1, 6):
vals_list.append(x*int(self.parent_value))
return tuple(zip(vals_list, vals_list))
or like this::
class AjaxChainedView(ChainedSelectChoicesView):
def get_child_set(self):
return ChildModel.object.filter(parent_id=self.parent_value)
Don't forget to update your urls.py::
url(r'^ajax/custom-chained-view-url/$', AjaxChainedView.as_view(), name='ajax_chained_view'),
Authors
-------
Library is by `Erik Telepovsky` from `Pragmatic Mates`_. See `our other libraries`_.
.. _Pragmatic Mates: http://www.pragmaticmates.com/
.. _our other libraries: https://github.com/PragmaticMates
Raw data
{
"_id": null,
"home_page": "https://github.com/PragmaticMates/django-clever-selects",
"name": "django-clever-selects",
"maintainer": "Pragmatic Mates",
"docs_url": null,
"requires_python": null,
"maintainer_email": "info@pragmaticmates.com",
"keywords": "django clever chained selects ajax",
"author": "Pragmatic Mates",
"author_email": "info@pragmaticmates.com",
"download_url": "https://files.pythonhosted.org/packages/d0/38/044bc8208292fe26a066066a6fca3f44058bddf8448aa8ddc0e47141b1c3/django_clever_selects-1.1.0.tar.gz",
"platform": null,
"description": "django-clever-selects\n=====================\n\nThis library is inspired by **django-chained-selectbox** from *s-block*\n(https://github.com/s-block/django-chained-selectbox).\n\nIt serves chained select box widget for Django framework using AJAX requests for chaining select boxes together.\nThe values change depending on the parent value.\n\nPrevious mentioned library was intended for use in Django admin only. The new library has frontend functionality,\nimproved existing instance data initialization and new ``ChainedModelChoiceField``. It also uses custom TestClient which\nis able pass ``request.user`` variable into AJAX view if user is logged in. It is very useful if you need to filter result queryset by\nuser permissions for example.\n\nTested on Django 1.4.5.\n\n\nRequirements\n------------\n- Django\n\n- jQuery\n\n\nInstallation\n------------\n\n1. Install python library using pip: pip install django-clever-selects\n\n2. Add ``clever_selects`` to ``INSTALLED_APPS`` in your Django settings file\n\n3. Add ``clever_selects_extras`` to your ``{% load %}`` statement and put ``{% clever_selects_js_import %}`` tag before closing ``</body>`` element. It is important to load clever-selects.js file after body content, so do not put it in the <head></head>!\n\n\nUsage\n-----\n\nForms\n'''''\n\nForm must inherit from ``ChainedChoicesMixin`` (or from ``ChainedChoicesForm`` / ``ChainedChoicesModelForm``, depends on your needs)\nwhich loads the options when there is already an instance or initial data::\n\n from clever_selects.form_fields import ChainedChoiceField\n from clever_selects.forms import ChainedChoicesForm\n\n\n class SimpleChainForm(ChainedChoicesForm):\n first_field = ChoiceField(choices=(('', '------------'), (1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5'), ))\n second_field = ChainedChoiceField(parent_field='first_field', ajax_url=reverse_lazy('ajax_chained_view'))\n\n\n class MultipleChainForm(ChainedChoicesForm):\n first_field = ChoiceField(choices=(('', '------------'), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), ))\n second_field = ChainedChoiceField(parent_field='first_field', ajax_url=reverse_lazy('ajax_chained_view'))\n third_field = ChainedChoiceField(parent_field='second_field', ajax_url=reverse_lazy('ajax_chained_view'))\n fourth_field = ChainedChoiceField(parent_field='third_field', ajax_url=reverse_lazy('ajax_chained_view'))\n fifth_field = ChainedChoiceField(parent_field='fourth_field', ajax_url=reverse_lazy('ajax_chained_view'))\n\n\n class ModelChainForm(ChainedChoicesModelForm):\n brand = forms.ModelChoiceField(queryset=CarBrand.objects.all(), required=True,\n empty_label=_('Select a car brand'))\n model = ChainedModelChoiceField(parent_field='brand', ajax_url=reverse_lazy('ajax_chained_models'),\n empty_label=_('Select a car model'), model=BrandModel, required=True)\n engine = forms.ChoiceField(choices=([('', _('All engine types'))] + Car.ENGINES), required=False)\n color = ChainedChoiceField(parent_field='model', ajax_url=reverse_lazy('ajax_chained_colors'),\n empty_label=_('Select a car model'), required=False)\n\n class Meta:\n model = Car\n\n\nNotice that ajax URLs could differ of each field for different purposes. See example project for more use cases.\n\nIn order to pre-populate child fields, the form can need to have access to the current user. This can be done by passing\nthe user to the kwargs of the form's __init__() method in the form's view. The ChainedSelectFormViewMixin takes care\nof this for you.::\n\n class CreateCarView(ChainedSelectFormViewMixin, CreateView)\n template_name = \"create_car.html\"\n form_class = ModelChainForm\n model = Car\n\nViews\n'''''\n\nAjax call is made whenever the parent field is changed. You must set up the ajax URL to return json list of lists::\n\n class AjaxChainedView(BaseDetailView):\n \"\"\"\n View to handle the ajax request for the field options.\n \"\"\"\n\n def get(self, request, *args, **kwargs):\n field = request.GET.get('field')\n parent_value = request.GET.get(\"parent_value\")\n\n vals_list = []\n for x in range(1, 6):\n vals_list.append(x*int(parent_value))\n\n choices = tuple(zip(vals_list, vals_list))\n\n response = HttpResponse(\n json.dumps(choices, cls=DjangoJSONEncoder),\n mimetype='application/javascript'\n )\n add_never_cache_headers(response)\n return response\n\n\nOr you can use ``ChainedSelectChoicesView`` class helper like this::\n\n class AjaxChainedView(ChainedSelectChoicesView):\n def get_choices(self):\n vals_list = []\n for x in range(1, 6):\n vals_list.append(x*int(self.parent_value))\n return tuple(zip(vals_list, vals_list))\n\nor like this::\n\n class AjaxChainedView(ChainedSelectChoicesView):\n def get_child_set(self):\n return ChildModel.object.filter(parent_id=self.parent_value)\n\nDon't forget to update your urls.py::\n\n url(r'^ajax/custom-chained-view-url/$', AjaxChainedView.as_view(), name='ajax_chained_view'),\n\nAuthors\n-------\n\nLibrary is by `Erik Telepovsky` from `Pragmatic Mates`_. See `our other libraries`_.\n\n.. _Pragmatic Mates: http://www.pragmaticmates.com/\n.. _our other libraries: https://github.com/PragmaticMates\n",
"bugtrack_url": null,
"license": "BSD License",
"summary": "Chained select box widget for Django framework using AJAX requests.",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/PragmaticMates/django-clever-selects"
},
"split_keywords": [
"django",
"clever",
"chained",
"selects",
"ajax"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d038044bc8208292fe26a066066a6fca3f44058bddf8448aa8ddc0e47141b1c3",
"md5": "2280842cc06ad8cd213c4e3eec1f24c8",
"sha256": "5dd64870448fde6819d51d0089b1c3b5b1de371b3eed7a022e70dc41d7cddab3"
},
"downloads": -1,
"filename": "django_clever_selects-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "2280842cc06ad8cd213c4e3eec1f24c8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11888,
"upload_time": "2024-11-22T09:33:01",
"upload_time_iso_8601": "2024-11-22T09:33:01.331508Z",
"url": "https://files.pythonhosted.org/packages/d0/38/044bc8208292fe26a066066a6fca3f44058bddf8448aa8ddc0e47141b1c3/django_clever_selects-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-22 09:33:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "PragmaticMates",
"github_project": "django-clever-selects",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-clever-selects"
}