django-js-choices


Namedjango-js-choices JSON
Version 0.5.1 PyPI version JSON
download
home_pagehttps://github.com/lorinkoz/django-js-choices
SummaryJavascript model field choices handling for Django.
upload_time2023-08-25 22:12:40
maintainer
docs_urlNone
authorLorenzo Peña
requires_python>=3.8.1,<4.0.0
licenseMIT
keywords django choices javascript
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            django-js-choices
=================

.. image:: https://img.shields.io/badge/packaging-poetry-purple.svg
    :alt: Packaging: poetry
    :target: https://github.com/sdispater/poetry

.. image:: https://img.shields.io/badge/code%20style-black-black.svg
    :alt: Code style: black
    :target: https://github.com/ambv/black

.. image:: https://github.com/lorinkoz/django-js-choices/workflows/code/badge.svg
    :alt: Build status
    :target: https://github.com/lorinkoz/django-js-choices/actions

.. image:: https://coveralls.io/repos/github/lorinkoz/django-js-choices/badge.svg?branch=master
    :alt: Code coverage
    :target: https://coveralls.io/github/lorinkoz/django-js-choices?branch=master

.. image:: https://badge.fury.io/py/django-js-choices.svg
    :alt: PyPi version
    :target: http://badge.fury.io/py/django-js-choices

.. image:: https://pepy.tech/badge/django-js-choices/month
    :alt: Downloads
    :target: https://pepy.tech/project/django-js-choices

|

Overview
--------

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

.. _model field choices: https://docs.djangoproject.com/en/dev/ref/models/fields.html#django.db.models.Field.choices

For example, given the model...

.. code-block:: python

    # 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.

.. code-block:: javascript

    Choices.pairs("year_in_school");

Result:

.. code-block:: javascript

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

Display values are also accesible.

.. code-block:: javascript

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

In both cases the result is

.. code-block:: javascript

    "Freshman"


Installation
------------

Install using ``pip``...

.. code-block:: bash

    pip install django-js-choices

...or clone the project from GitHub.

.. code-block:: bash

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

Add ``'django_js_choices'`` to your ``INSTALLED_APPS`` setting.

.. code-block:: python

    INSTALLED_APPS = (
        ...
        'django_js_choices',
    )


Usage as static file
--------------------

First generate static file by

.. code-block:: bash

    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 created with the locale prefix defined in your settings,
but you can pass any locale to the command...

.. code-block:: bash

    python manage.py collectstatic_js_choices --locale es

In this case, the generated file will be ``choices-es.js``.

After this add the file to your template.

.. code-block:: html

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


Usage with views
----------------

Include non-cached view...

.. code-block:: python

    from django_js_choices.views import choices_js

    urlpatterns = [
        path("jschoices/", choices_js, name="js_choices"),
    ]

...or use cache to save some bandwith.

.. code-block:: python

    from django_js_choices.views import choices_js

    urlpatterns = [
        path("jschoices/", cache_page(3600)(choices_js), name="js_choices"),
    ]

Include javascript in your template.

.. code-block:: html

    <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.

.. code-block:: html

    {% 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.

.. code-block:: javascript

    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.


Register other choices not in models
------------------------------------

If you have some choices you want to expose to javascript, and they don't fit into any
model, lets say for example, a list of actions, you can add those too:

.. code-block:: python

    from django_js_choices.core import register_choice

    POSSIBLE_ACTIONS = (
        ("go_down", "Go down"),
        ("go_top", "Go top"),
        ("nothing", "Stay")
    )
    ...
    # register_choice(name: string, choices: list)
    register_choice("possible_actions", POSSIBLE_ACTIONS)

- If any of the names of a manually registered choice conflicts with some model fields, the one you manually
  registered will be the one you'll access.
- The 2nd argument is the same type that you'd pass to a CharField `choices` argument.

You can only access the ``POSSIBLE_ACTIONS`` choices through the name ``possible_actions``

PLEASE NOTE: You must ensure the file where you are registering your choice is been processed by django

.. code-block:: javascript

    Choices.pairs("possible_actions")


Options
-------

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

.. code-block:: python

    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``.

.. code-block:: python

    JS_CHOICES_JS_GLOBAL_OBJECT_NAME = 'window'

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

.. code-block:: python

    JS_CHOICES_JS_MINIFY = False


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 <https://github.com/ierror/django-js-reverse>`_


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/lorinkoz/django-js-choices",
    "name": "django-js-choices",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8.1,<4.0.0",
    "maintainer_email": "",
    "keywords": "django,choices,javascript",
    "author": "Lorenzo Pe\u00f1a",
    "author_email": "lorinkoz@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f3/14/9f1a8f002b5dd39ab93ad97e8cc052bf0685f2a8d00b965ff232b1673e03/django_js_choices-0.5.1.tar.gz",
    "platform": null,
    "description": "django-js-choices\n=================\n\n.. image:: https://img.shields.io/badge/packaging-poetry-purple.svg\n    :alt: Packaging: poetry\n    :target: https://github.com/sdispater/poetry\n\n.. image:: https://img.shields.io/badge/code%20style-black-black.svg\n    :alt: Code style: black\n    :target: https://github.com/ambv/black\n\n.. image:: https://github.com/lorinkoz/django-js-choices/workflows/code/badge.svg\n    :alt: Build status\n    :target: https://github.com/lorinkoz/django-js-choices/actions\n\n.. image:: https://coveralls.io/repos/github/lorinkoz/django-js-choices/badge.svg?branch=master\n    :alt: Code coverage\n    :target: https://coveralls.io/github/lorinkoz/django-js-choices?branch=master\n\n.. image:: https://badge.fury.io/py/django-js-choices.svg\n    :alt: PyPi version\n    :target: http://badge.fury.io/py/django-js-choices\n\n.. image:: https://pepy.tech/badge/django-js-choices/month\n    :alt: Downloads\n    :target: https://pepy.tech/project/django-js-choices\n\n|\n\nOverview\n--------\n\nDjango JS Choices makes handling of `model field choices`_ in javascript easy.\n\n.. _model field choices: https://docs.djangoproject.com/en/dev/ref/models/fields.html#django.db.models.Field.choices\n\nFor example, given the model...\n\n.. code-block:: python\n\n    # models.py:\n\n    class Student(models.Model):\n        FRESHMAN = 'FR'\n        SOPHOMORE = 'SO'\n        JUNIOR = 'JR'\n        SENIOR = 'SR'\n        YEAR_IN_SCHOOL_CHOICES = (\n            (FRESHMAN, 'Freshman'),\n            (SOPHOMORE, 'Sophomore'),\n            (JUNIOR, 'Junior'),\n            (SENIOR, 'Senior'),\n        )\n        year_in_school = models.CharField(\n            max_length=2,\n            choices=YEAR_IN_SCHOOL_CHOICES,\n            default=FRESHMAN,\n        )\n\n...the choices are accesible in javascript.\n\n.. code-block:: javascript\n\n    Choices.pairs(\"year_in_school\");\n\nResult:\n\n.. code-block:: javascript\n\n    [\n        {value: \"FR\", label: \"Freshman\"},\n        {value: \"SO\", label: \"Sophomore\"},\n        {value: \"JR\", label: \"Junior\"},\n        {value: \"SR\", label: \"Senior\"}\n    ]\n\nDisplay values are also accesible.\n\n.. code-block:: javascript\n\n    Choices.display(\"year_in_school\", \"FR\")\n    Choices.display(\"year_in_school\", {\"year_in_school\": \"FR\"})\n\nIn both cases the result is\n\n.. code-block:: javascript\n\n    \"Freshman\"\n\n\nInstallation\n------------\n\nInstall using ``pip``...\n\n.. code-block:: bash\n\n    pip install django-js-choices\n\n...or clone the project from GitHub.\n\n.. code-block:: bash\n\n    git clone https://github.com/lorinkoz/django-js-choices.git\n\nAdd ``'django_js_choices'`` to your ``INSTALLED_APPS`` setting.\n\n.. code-block:: python\n\n    INSTALLED_APPS = (\n        ...\n        'django_js_choices',\n    )\n\n\nUsage as static file\n--------------------\n\nFirst generate static file by\n\n.. code-block:: bash\n\n    python manage.py collectstatic_js_choices\n\nIf you add apps, models, or change some existing choices,\nyou may update the choices.js file by running the command again.\n\nThe choices files is created with the locale prefix defined in your settings,\nbut you can pass any locale to the command...\n\n.. code-block:: bash\n\n    python manage.py collectstatic_js_choices --locale es\n\nIn this case, the generated file will be ``choices-es.js``.\n\nAfter this add the file to your template.\n\n.. code-block:: html\n\n    <script src=\"{% static 'choices-es.js' %}\"></script>\n\n\nUsage with views\n----------------\n\nInclude non-cached view...\n\n.. code-block:: python\n\n    from django_js_choices.views import choices_js\n\n    urlpatterns = [\n        path(\"jschoices/\", choices_js, name=\"js_choices\"),\n    ]\n\n...or use cache to save some bandwith.\n\n.. code-block:: python\n\n    from django_js_choices.views import choices_js\n\n    urlpatterns = [\n        path(\"jschoices/\", cache_page(3600)(choices_js), name=\"js_choices\"),\n    ]\n\nInclude javascript in your template.\n\n.. code-block:: html\n\n    <script src=\"{% url 'js_choices' %}\" type=\"text/javascript\"></script>\n\n\nUsage as template tag\n---------------------\n\nIf you want to generate the javascript code inline, use the template tag.\n\n.. code-block:: html\n\n    {% load js_choices %}\n    <script type=\"text/javascript\" charset=\"utf-8\">\n        {% js_choices_inline %}\n    </script>\n\n\nUse the choices in javascript\n-----------------------------\n\nFor every model field with choices, they will be available by the following\nnames.\n\n.. code-block:: javascript\n\n    Choices.pairs(\"<app_label>_<model_name>_<field_name>\")\n    Choices.pairs(\"<model_name>_<field_name>\")\n    Choices.pairs(\"<field_name>\")\n\nIf any of these names conflict with other model fields,\nthe conflicting names won't be accessible to prevent ambiguity.\n\n\nRegister other choices not in models\n------------------------------------\n\nIf you have some choices you want to expose to javascript, and they don't fit into any\nmodel, lets say for example, a list of actions, you can add those too:\n\n.. code-block:: python\n\n    from django_js_choices.core import register_choice\n\n    POSSIBLE_ACTIONS = (\n        (\"go_down\", \"Go down\"),\n        (\"go_top\", \"Go top\"),\n        (\"nothing\", \"Stay\")\n    )\n    ...\n    # register_choice(name: string, choices: list)\n    register_choice(\"possible_actions\", POSSIBLE_ACTIONS)\n\n- If any of the names of a manually registered choice conflicts with some model fields, the one you manually\n  registered will be the one you'll access.\n- The 2nd argument is the same type that you'd pass to a CharField `choices` argument.\n\nYou can only access the ``POSSIBLE_ACTIONS`` choices through the name ``possible_actions``\n\nPLEASE NOTE: You must ensure the file where you are registering your choice is been processed by django\n\n.. code-block:: javascript\n\n    Choices.pairs(\"possible_actions\")\n\n\nOptions\n-------\n\nOptionally, you can overwrite the default javascript variable 'Choices' used\nto access the choices by Django setting.\n\n.. code-block:: python\n\n    JS_CHOICES_JS_VAR_NAME = 'Choices'\n\nOptionally, you can change the name of the global object the javascript\nvariable used to access the choices is attached to. Default is ``this``.\n\n.. code-block:: python\n\n    JS_CHOICES_JS_GLOBAL_OBJECT_NAME = 'window'\n\nOptionally, you can disable the minfication of the generated javascript file\nby Django setting.\n\n.. code-block:: python\n\n    JS_CHOICES_JS_MINIFY = False\n\n\nContributing\n------------\n\n- PRs are welcome!\n- To run the test suite run ``make`` or ``make coverage``. The tests for this\n  project live inside a small django project called ``djsc_sandbox``.\n\n\nCredits\n-------\n\nInspired by (and conceptually forked from)\n`django-js-reverse <https://github.com/ierror/django-js-reverse>`_\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Javascript model field choices handling for Django.",
    "version": "0.5.1",
    "project_urls": {
        "Homepage": "https://github.com/lorinkoz/django-js-choices",
        "Repository": "https://github.com/lorinkoz/django-js-choices"
    },
    "split_keywords": [
        "django",
        "choices",
        "javascript"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e0753c9712d6096d42834bd628944a448bb858f8267028d68dbd583023b3a2ff",
                "md5": "b060d71855ec2d09702f5be958cfd069",
                "sha256": "473beac9f714126fb0d3b20b781729f1259ded134d2dfb4d03cd0c8fd86bc664"
            },
            "downloads": -1,
            "filename": "django_js_choices-0.5.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b060d71855ec2d09702f5be958cfd069",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 8725,
            "upload_time": "2023-08-25T22:12:38",
            "upload_time_iso_8601": "2023-08-25T22:12:38.964290Z",
            "url": "https://files.pythonhosted.org/packages/e0/75/3c9712d6096d42834bd628944a448bb858f8267028d68dbd583023b3a2ff/django_js_choices-0.5.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f3149f1a8f002b5dd39ab93ad97e8cc052bf0685f2a8d00b965ff232b1673e03",
                "md5": "52d9fa3241859f0b76b0abaee7efde4a",
                "sha256": "12ae2ac329e87a7e554654ad3c8b4eb561e95d2d547fab8315b75c02ed21e0e7"
            },
            "downloads": -1,
            "filename": "django_js_choices-0.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "52d9fa3241859f0b76b0abaee7efde4a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 8468,
            "upload_time": "2023-08-25T22:12:40",
            "upload_time_iso_8601": "2023-08-25T22:12:40.887466Z",
            "url": "https://files.pythonhosted.org/packages/f3/14/9f1a8f002b5dd39ab93ad97e8cc052bf0685f2a8d00b965ff232b1673e03/django_js_choices-0.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-25 22:12:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lorinkoz",
    "github_project": "django-js-choices",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "django-js-choices"
}
        
Elapsed time: 0.10306s