{"id":13411506,"url":"https://github.com/lorinkoz/django-js-choices","last_synced_at":"2025-06-19T11:33:47.692Z","repository":{"id":43409771,"uuid":"93749850","full_name":"lorinkoz/django-js-choices","owner":"lorinkoz","description":"Javascript model field's choices handling for Django","archived":false,"fork":false,"pushed_at":"2024-10-15T18:38:56.000Z","size":76,"stargazers_count":18,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-29T07:19:34.712Z","etag":null,"topics":["choices","django","javascript","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lorinkoz.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-08T13:00:06.000Z","updated_at":"2024-10-15T16:02:34.000Z","dependencies_parsed_at":"2024-10-16T18:37:24.599Z","dependency_job_id":null,"html_url":"https://github.com/lorinkoz/django-js-choices","commit_stats":{"total_commits":52,"total_committers":3,"mean_commits":"17.333333333333332","dds":"0.11538461538461542","last_synced_commit":"824ca8f526d2d58541038726d47f1430639035d7"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/lorinkoz/django-js-choices","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorinkoz%2Fdjango-js-choices","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorinkoz%2Fdjango-js-choices/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorinkoz%2Fdjango-js-choices/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorinkoz%2Fdjango-js-choices/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lorinkoz","download_url":"https://codeload.github.com/lorinkoz/django-js-choices/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorinkoz%2Fdjango-js-choices/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260742409,"owners_count":23055761,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["choices","django","javascript","python"],"created_at":"2024-07-30T20:01:14.130Z","updated_at":"2025-06-19T11:33:42.679Z","avatar_url":"https://github.com/lorinkoz.png","language":"Python","funding_links":[],"categories":["Web Development"],"sub_categories":["Python"],"readme":"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    \u003cscript src=\"{% static 'choices-es.js' %}\"\u003e\u003c/script\u003e\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    \u003cscript src=\"{% url 'js_choices' %}\" type=\"text/javascript\"\u003e\u003c/script\u003e\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    \u003cscript type=\"text/javascript\" charset=\"utf-8\"\u003e\n        {% js_choices_inline %}\n    \u003c/script\u003e\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(\"\u003capp_label\u003e_\u003cmodel_name\u003e_\u003cfield_name\u003e\")\n    Choices.pairs(\"\u003cmodel_name\u003e_\u003cfield_name\u003e\")\n    Choices.pairs(\"\u003cfield_name\u003e\")\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 ``sandbox``.\n\n\nCredits\n-------\n\nInspired by (and conceptually forked from)\n`django-js-reverse \u003chttps://github.com/ierror/django-js-reverse\u003e`_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florinkoz%2Fdjango-js-choices","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florinkoz%2Fdjango-js-choices","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florinkoz%2Fdjango-js-choices/lists"}