{"id":21328768,"url":"https://github.com/annefly/django-dynamic-formset-select2-poc","last_synced_at":"2025-07-12T08:32:09.177Z","repository":{"id":148495054,"uuid":"45941793","full_name":"anneFly/django-dynamic-formset-select2-poc","owner":"anneFly","description":"Working example that django-select2 can work with django-dynamic-formset","archived":false,"fork":false,"pushed_at":"2015-11-26T21:28:14.000Z","size":16,"stargazers_count":3,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T16:23:37.221Z","etag":null,"topics":["django-dynamic-formset","django-select2"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anneFly.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-10T21:36:13.000Z","updated_at":"2022-06-13T15:00:30.000Z","dependencies_parsed_at":"2023-04-14T08:24:30.119Z","dependency_job_id":null,"html_url":"https://github.com/anneFly/django-dynamic-formset-select2-poc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/anneFly/django-dynamic-formset-select2-poc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anneFly%2Fdjango-dynamic-formset-select2-poc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anneFly%2Fdjango-dynamic-formset-select2-poc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anneFly%2Fdjango-dynamic-formset-select2-poc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anneFly%2Fdjango-dynamic-formset-select2-poc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anneFly","download_url":"https://codeload.github.com/anneFly/django-dynamic-formset-select2-poc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anneFly%2Fdjango-dynamic-formset-select2-poc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264962376,"owners_count":23689803,"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":["django-dynamic-formset","django-select2"],"created_at":"2024-11-21T21:41:41.994Z","updated_at":"2025-07-12T08:32:08.913Z","avatar_url":"https://github.com/anneFly.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-select2 \u0026 django-dynamic-formset demo\nWorking example that [django-select2](https://github.com/applegrew/django-select2) can work with [django-dynamic-formset](https://github.com/elo80ka/django-dynamic-formset).\n\nIf you have trouble setting up django-select2 and django-dynamic-formset together, this repository might help you.\nCheckout [this example](https://github.com/anneFly/django-dynamic-formset-select2-poc/blob/master/poc/app/templates/book-list.html).\nIt contains everything you need. There is also [another example](https://github.com/anneFly/django-dynamic-formset-select2-poc/blob/master/poc/app/templates/book-list-multiple.html)\nshowing several formsets one page.\n\n## Step by Step\nLet's assume your formset is called `form` in your template context.\n\n1) Create a hidden container for your template and render an `empty_form` inside. Give your container a class or an id so that you can use it later in the javascript, e.g. `id=\"formset-template\"`\n\n```\n\u003cdiv id=\"formset-template\" style=\"display: none;\"\u003e\n    {{ form.empty_form }}\n\u003c/div\u003e\n```\n\n\n2) Render your formset inside some container that has a class or id which you can later use for initializing the dynamic formset. In this example it had the id \"formset\":\n```\n\u003cform\u003e\n    {{ form.management_form }}\n    \u003cdiv id=\"formset\"\u003e\n        {% for f in form %}\n            {{ f }}\n        {% endfor %}\n    \u003c/div\u003e\n    \u003cbutton type=\"submit\"\u003esubmit\u003c/button\u003e\n\u003c/form\u003e\n```\n\n\n3) Include jQuery and the django-dynamic-form js\n```\n\u003cscript src=\"//code.jquery.com/jquery-2.1.4.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"{% static 'jquery.formset.js' %}\"\u003e\u003c/script\u003e\n```\n\n4) Include form.media for django-select2 to work\nIn your html `head` include `{{ form.media.css }}`\nAt the end of your `body` include `{{ form.media.js }}`\n\n5) Initialize dynamic-formset\nHere is important, that you do pass a __copy__ of your formset template element: `formTemplate: $('#formset-template').clone()`.\nIn the `added` callback of django-dynamic-form you need to initialize your newly created form by calling `djangoSelect2()` on the new django-select2 element.\nThe whole code could look like this:\n```\n$('#formset').formset({\n    formTemplate: $('#formset-template').clone(),\n    added: function (row) {\n        row.find('.django-select2').djangoSelect2();\n    }\n});\n```\n\n__Important__: It is necessary that the code from step 5 runs before the code from `{{ form.media.js }}`. This should automatically be the case if you do not wrap your code insde a jQuery `$(document).ready`. However, if you do want to wrap this code inside a `$(document).ready` then include it __before__ `{{ form.media.js }}`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fannefly%2Fdjango-dynamic-formset-select2-poc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fannefly%2Fdjango-dynamic-formset-select2-poc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fannefly%2Fdjango-dynamic-formset-select2-poc/lists"}