Skip to main content

Javascript model field choices handling for Django.

Project description

Packaging: poetry Code style: black Build status Code coverage PyPi version Downloads

Overview

Django JS Choices makes handling of model field choices in javascript easy.

For example, given the model…

# models.py:

class Student(models.Model):
    FRESHMAN = 'FR'
    SOPHOMORE = 'SO'
    JUNIOR = 'JR'
    SENIOR = 'SR'
    YEAR_IN_SCHOOL_CHOICES = (
        (FRESHMAN, 'Freshman'),
        (SOPHOMORE, 'Sophomore'),
        (JUNIOR, 'Junior'),
        (SENIOR, 'Senior'),
    )
    year_in_school = models.CharField(
        max_length=2,
        choices=YEAR_IN_SCHOOL_CHOICES,
        default=FRESHMAN,
    )

…the choices are accesible in javascript.

Choices.pairs("year_in_school");

Result:

[
    {value: "FR", label: "Freshman"},
    {value: "SO", label: "Sophomore"},
    {value: "JR", label: "Junior"},
    {value: "SR", label: "Senior"}
]

Display values are also accesible.

Choices.display("year_in_school", "FR")
Choices.display("year_in_school", {"year_in_school": "FR"})

In both cases the result is

"Freshman"

Installation

Install using pip

pip install django-js-choices

…or clone the project from GitHub.

git clone https://github.com/lorinkoz/django-js-choices.git

Add 'django_js_choices' to your INSTALLED_APPS setting.

INSTALLED_APPS = (
    ...
    'django_js_choices',
)

Usage as static file

First generate static file by

python manage.py collectstatic_js_choices

If you add apps, models, or change some existing choices, you may update the choices.js file by running the command again.

The choices files is always created with a locale prefix: choices-en-us.js but you can pass any locale to the command…

python manage.py collectstatic_js_choices --locale es

…and the generated file will be choices-es.js

After this add the file to your template.

<script src="{% static 'django_js_choices/js/choices-es.js' %}"></script>

Usage with views

Include non-cached view…

from django_js_choices.views import choices_js
urlpatterns = [
    url(r'^jschoices/$', choices_js, name='js_choices'),
]

…or use cache to save some bandwith.

from django_js_choices.views import choices_js

urlpatterns = [
    url(r'^jschoices/$', cache_page(3600)(choices_js), name='js_choices'),
]

Include javascript in your template.

<script src="{% url 'js_choices' %}" type="text/javascript"></script>

Usage as template tag

If you want to generate the javascript code inline, use the template tag.

{% load js_choices %}
<script type="text/javascript" charset="utf-8">
    {% js_choices_inline %}
</script>

Use the choices in javascript

For every model field with choices, they will be available by the following names.

Choices.pairs("<app_label>_<model_name>_<field_name>")
Choices.pairs("<model_name>_<field_name>")
Choices.pairs("<field_name>")

If any of these names conflict with other model fields, the conflicting names won’t be accessible to prevent ambiguity.

Options

Optionally, you can overwrite the default javascript variable ‘Choices’ used to access the choices by Django setting.

JS_CHOICES_JS_VAR_NAME = 'Choices'

Optionally, you can change the name of the global object the javascript variable used to access the choices is attached to. Default is this.

JS_CHOICES_JS_GLOBAL_OBJECT_NAME = 'window'

Optionally, you can disable the minfication of the generated javascript file by Django setting.

JS_CHOICES_JS_MINIFY = False

By default collectstatic_js_choices writes its output (choices-en-us.js) to your project’s STATIC_ROOT, but you can change the output path.

JS_CHOICES_OUTPUT_PATH = 'some/other/path'

Contributing

  • PRs are welcome!

  • To run the test suite run make or make coverage. The tests for this project live inside a small django project called djsc_sandbox.

Credits

Inspired by (and conceptually forked from) django-js-reverse

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-js-choices-0.3.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_js_choices-0.3.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file django-js-choices-0.3.0.tar.gz.

File metadata

  • Download URL: django-js-choices-0.3.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for django-js-choices-0.3.0.tar.gz
Algorithm Hash digest
SHA256 90e87cfbe0f8e62f91a858929919894378993a968e5be2bb8d3786c50a0282ab
MD5 307a5ae09c68d4dde82de8c2c59c787a
BLAKE2b-256 1403b0751077d71637a1d6f314cd565a83e6b07dc1b40ae4ba743415f5e8563f

See more details on using hashes here.

File details

Details for the file django_js_choices-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_js_choices-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for django_js_choices-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e592ab05cc8d2de44ac9e27f078fdf67df7bffaee1f130bd41e31663e61c1724
MD5 dd82c1495f1620cc99f22c9605ba8afb
BLAKE2b-256 d36bc5fedd233bd1c7ef0549d8322467e3ed2f4b734ad4b3ea87a8ca62985a20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page