-
Notifications
You must be signed in to change notification settings - Fork 315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The AutoModelSelect2Field does not work for new added form when used with admin.StackedInline #32
Comments
It is hard to say. What is the js code you are talking about? Also it would be helpful if u have a demo page to show your problem. |
Maybe I did not express my problem exactly , so I forked the django-select2 and modify the testapp to give a example, you can find it here: https://github.com/yflau/django-select2 If you run the example, you need to login django admin with username http://127.0.0.1:8000/admin/testmain/dept/add/ the screen look like this: here we can use the word field normally, then click the and the word field of the new added Employee form will not work. |
There doesn't seem to be an easy way. The admin JS makes an empty clone which it uses to replicate when new rows are added. However it simply clones, not the event-handlers, only the DOMs and data associated. Still not sure how to handle this. |
The only way I see of fixing this.. Develop a new Admin portal, where on clicking Add new row, instead of cloning the DOMs it would make an Ajax call to server. The server would then just render only the row and return that. The JS on Admin side would then have to simply insert that html code in a new row. |
Good news! I spent 4 hours on this issue today. And I finally managed to make a workaround: https://gist.github.com/BertrandBordage/8288704. Of course, it's totally unperfect, but I wanted to point out the fact that we can get it working without any modification on Django itself. |
Cool. The best solution is to create custom add and list pages for this library. |
Are you sure that would be the best solution? Adding custom ModelAdmins in django-select2 means even complex setups when using several third-party apps (like django-reversion, django-polymorphic, django-mptt, etc). |
I did not try that, but seems to be the cleanest approach. Due to the nature of this library it needs to depend on some hacky JS codes. The best way to handle is to have custom page which is aware of that and takes care to not break those codes. Unless and until there are other third party hacky apps which too needs to inject some JS code into admin pages to get them working, then we are okay. |
* Previously, when using {{ form.empty_form }} the inline js would automatically be called on the `empty_form`. This made it impossible to dynamically add inlines to the page because of how `__prefix__` is duplicated to the new inlines. This commit wraps all the inline js in a function which is attached to global `window.django_select2` plugin. On page load only non `empty_form`'s are initialized with select2, giving the developer to attach to the 'add new inline' click and call the `django_select2` plugin with the proper inline formset id. I am using this now with `django-superformset` and dynamically inserted inlines that contain select2 fields are working as expected. Additionally all the inline js for each formset is identicaly, a future cleanup could be to only inline the field and form id variables. * First converted `widgets.py` to Unix file type so the diff does not contain windows line endings. * Since all the inline js is now run post page load, we can put all of the`django_select2` js libraries at the page bottom with other js assets. refs applegrew#51 * As a bonus, this removes all the js code generation libs from utils with json.dumps(). * I have not tried formsets with the django admin, however this work will allow inline formsets support to be added to the admin with less developer effort than before. Refs: applegrew#125, applegrew#65, applegrew#49, applegrew#32, applegrew#109
* Previously, when using {{ form.empty_form }} the inline js would automatically be called on the `empty_form`. This made it impossible to dynamically add inlines to the page because of how `__prefix__` is duplicated to the new inlines. This commit wraps all the inline js in a function which is attached to global `window.django_select2` plugin. On page load only non `empty_form`'s are initialized with select2, giving the developer to attach to the 'add new inline' click and call the `django_select2` plugin with the proper inline formset id. I am using this now with `django-superformset` and dynamically inserted inlines that contain select2 fields are working as expected. Additionally all the inline js for each formset is identicaly, a future cleanup could be to only inline the field and form id variables. * First converted `widgets.py` to Unix file type so the diff does not contain windows line endings. * Since all the inline js is now run post page load, we can put all of the`django_select2` js libraries at the page bottom with other js assets. refs applegrew#51 * As a bonus, this removes all the js code generation libs from utils with json.dumps(). * I have not tried formsets with the django admin, however this work will allow inline formsets support to be added to the admin with less developer effort than before. Refs: applegrew#125, applegrew#65, applegrew#49, applegrew#32, applegrew#109
* Previously, when using {{ form.empty_form }} the inline js would automatically be called on the `empty_form`. This made it impossible to dynamically add inlines to the page because of how `__prefix__` is duplicated to the new inlines. This commit wraps all the inline js in a function which is attached to global `window.django_select2` plugin. On page load only non `empty_form`'s are initialized with select2, giving the developer to attach to the 'add new inline' click and call the `django_select2` plugin with the proper inline formset id. I am using this now with `django-superformset` and dynamically inserted inlines that contain select2 fields are working as expected. Additionally all the inline js for each formset is identicaly, a future cleanup could be to only inline the field and form id variables. * First converted `widgets.py` to Unix file type so the diff does not contain windows line endings. * Since all the inline js is now run post page load, we can put all of the`django_select2` js libraries at the page bottom with other js assets. refs applegrew#51 * As a bonus, this removes all the js code generation libs from utils with json.dumps(). * I have not tried formsets with the django admin, however this work will allow inline formsets support to be added to the admin with less developer effort than before. Refs: applegrew#125, applegrew#65, applegrew#49, applegrew#32, applegrew#109
I want to use the AutoModelSelect2Field in django admin within a inline form like this:
forms.py
admin.py
it is all ok but when I click the add another inline form, the AutoModelSelect2Field of the new added form does not work, I think I should use jquery live method to bind new add element, but I don't know how to do? Can you give me some hints, thanks
The text was updated successfully, but these errors were encountered: