Package: django-js-choices
OverviewDjango JS Choices makes handling ofmodel field choicesin javascript easy.For example, given the model…# models.py:classStudent(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"InstallationInstall usingpip…pipinstalldjango-js-choices…or clone the project from GitHub.gitclonehttps://github.com/lorinkoz/django-js-choices.gitAdd'django_js_choices'to yourINSTALLED_APPSsetting.INSTALLED_APPS=(...'django_js_choices',)Usage as static fileFirst generate static file bypythonmanage.pycollectstatic_js_choicesIf 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…pythonmanage.pycollectstatic_js_choices--localeesIn this case, the generated file will bechoices-es.js.After this add the file to your template.<scriptsrc="{% static 'choices-es.js' %}"></script>Usage with viewsInclude non-cached view…fromdjango_js_choices.viewsimportchoices_jsurlpatterns=[path("jschoices/",choices_js,name="js_choices"),]…or use cache to save some bandwith.fromdjango_js_choices.viewsimportchoices_jsurlpatterns=[path("jschoices/",cache_page(3600)(choices_js),name="js_choices"),]Include javascript in your template.<scriptsrc="{% url 'js_choices' %}"type="text/javascript"></script>Usage as template tagIf you want to generate the javascript code inline, use the template tag.{% load js_choices %}<scripttype="text/javascript"charset="utf-8">{%js_choices_inline%}</script>Use the choices in javascriptFor 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.Register other choices not in modelsIf 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:fromdjango_js_choices.coreimportregister_choicePOSSIBLE_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 CharFieldchoicesargument.You can only access thePOSSIBLE_ACTIONSchoices through the namepossible_actionsPLEASE NOTE: You must ensure the file where you are registering your choice is been processed by djangoChoices.pairs("possible_actions")OptionsOptionally, 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 isthis.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=FalseContributingPRs are welcome!To run the test suite runmakeormake coverage. The tests for this
project live inside a small django project calleddjsc_sandbox.CreditsInspired by (and conceptually forked from)django-js-reverse