Skip to content
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

Closed
yflau opened this issue Mar 13, 2013 · 8 comments

Comments

@yflau
Copy link

yflau commented Mar 13, 2013

I want to use the AutoModelSelect2Field in django admin within a inline form like this:

forms.py

class ProductChoices(AutoModelSelect2Field):
    queryset = Product.objects
    search_fields = ['name__icontains', ]

class OrderItemAdminForm(forms.ModelForm):
    product = ProductChoices(required=False, label=_("Product"))
    performlist = forms.DateTimeField(required=False, widget=PerformList)
    pricelist = forms.IntegerField(required=False, label=_("Price List"), widget=PriceList)
    class Meta:
        model = OrderItem

admin.py

class OrderItemInline(admin.StackedInline):
    form = OrderItemAdminForm
    model = OrderItem
    extra = 1
    fields = ('product', 'performlist', 'pricelist', 'price', 'quantity', 'receivable', 'received')
    raw_id_fields = ['price',]

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

@applegrew
Copy link
Owner

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.

@yflau
Copy link
Author

yflau commented Mar 13, 2013

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 admin and password admin , then
you can navigate to this url:

http://127.0.0.1:8000/admin/testmain/dept/add/

the screen look like this:

first

here we can use the word field normally, then click the Add another Employee link, the screen look like this:

second

and the word field of the new added Employee form will not work.

@applegrew
Copy link
Owner

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.

@applegrew
Copy link
Owner

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.

@BertrandBordage
Copy link

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.

@applegrew
Copy link
Owner

Cool. The best solution is to create custom add and list pages for this library.

@BertrandBordage
Copy link

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

@applegrew
Copy link
Owner

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.

rizumu added a commit to rizumu/django-select2 that referenced this issue Sep 11, 2014
  * 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
rizumu added a commit to rizumu/django-select2 that referenced this issue Sep 11, 2014
  * 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
rizumu added a commit to rizumu/django-select2 that referenced this issue Sep 11, 2014
  * 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
@rizumu rizumu mentioned this issue Sep 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants