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

3.5.1 new Media order is not safely compatible with ModelAdmin.autocomplete_fields #1143

Closed
raratiru opened this issue Mar 5, 2020 · 23 comments

Comments

@raratiru
Copy link
Contributor

raratiru commented Mar 5, 2020

In the following setup, one inline uses autocomplete_fields the other inline uses DAL.

It works in 3.5.0. In order to work in 3.5.1. the Media meta-calss should also be defined in the inline that uses autocomplete_fields.

class WithDALForm(forms.ModelForm):
    field_with_DAL = forms.ModelMultipleChoiceField(
        ...
        widget=autocomplete.ModelSelect2Multiple()
    )

    class Media:
        js = ("//code.jquery.com/jquery-3.4.1.min.js",)


class WithDALInline(admin.StackedInline):
    form = WithDALForm


class WithAutocompleteInline(admin.StackedInline)
    autocomplete_fields = ('field_1', 'field_2')


class Admin(admin.ModelAdmin):
    inlines = ('WithAutocompleteInline', 'WithDALIline')

What follows is the order of the scripts in each case:

3.5.1 with admin autocomplete (not working):

<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/select2/select2.full.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/select2.full.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/select2/i18n/en.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/i18n/en.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/inlines.js"></script>
<script type="text/javascript" src="/static/admin/js/autocomplete.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/forward.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/select2.js"></script>
<script type="text/javascript" src="/static/admin/js/collapse.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.post-setup.js"></script>
<script type="text/javascript" src="/static/admin/js/urlify.js"></script>
<script type="text/javascript" src="/static/admin/js/prepopulate.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/xregexp/xregexp.js"></script>

3.5.1 without admin autocomplete (working):

<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/select2.full.js"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/inlines.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/i18n/en.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/admin/js/collapse.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/forward.js"></script>
<script type="text/javascript" src="/static/admin/js/urlify.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/select2.js"></script>
<script type="text/javascript" src="/static/admin/js/prepopulate.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.post-setup.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/xregexp/xregexp.js"></script>

3.5.0 with admin autocomplete (working):

<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/select2/select2.full.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/select2/i18n/en.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/select2.full.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/i18n/en.js"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/inlines.js"></script>
<script type="text/javascript" src="/static/admin/js/autocomplete.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/forward.js"></script>
<script type="text/javascript" src="/static/admin/js/collapse.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/select2.js"></script>
<script type="text/javascript" src="/static/admin/js/urlify.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.post-setup.js"></script>
<script type="text/javascript" src="/static/admin/js/prepopulate.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/xregexp/xregexp.js"></script>
@jpic
Copy link
Member

jpic commented Mar 18, 2020

Does it work with #1147 ?

@jpic
Copy link
Member

jpic commented Mar 18, 2020

Any idea where this is coming from ?

<script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.min.js"></script>

@jpic
Copy link
Member

jpic commented Mar 18, 2020

Also published in : django-autocomplete-light==3.6.0-dev1

Please try it out, it is expected to break fix in theory, but to fix things in practice, regarding jquery et al.

@raratiru
Copy link
Contributor Author

raratiru commented Mar 19, 2020

I tried 3.6.0-dev1, the fix still works:

Insert jquery both:

  • in the inline-modelform that DAL is needed
  • in the inline-modelform where admin autocomplete is set

The expected(?) scenario of inserting jquery only in the modelform that uses DAL is still not working.

(?) Since I may be missing this:

Is there a single source of truth regarding the introduction of jquery in the admin, for DAL to work in ModelAdmin and its inlines?

<script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.min.js"></script> comes from class WithDALForm(forms.ModelForm).Media:

It is the class that defines a form field which uses DAL and its Media class brings jquery in the admin.

@jpic
Copy link
Member

jpic commented Mar 19, 2020

I thought that it would work as long as there's only one jquery loaded in the page, in which case you would not need WithDALForm anymore, does it work without WithDALForm ?

Outside the admin, I think that just loading jquery manually would work.

@raratiru
Copy link
Contributor Author

raratiru commented Mar 19, 2020

WithDAL(ModelForm), provides field_with_DAL which is not a model field (otherwise, I could use ModelAdmin.autocomplete_fields).

BACKWARD COMPATIBILITY BREAKS:
- jquery.js has been **removed** from widget.media, this means that you are
now responsible for ensuring that jquery is loaded in your page prior to
displaying the form (form.media).

From DAL-3.5.0, I need to load jquery manually, and I did it in WithDAL(ModelForm).Media because it is also being used in its change form (not only as inline).

The above works in 3.5.0.

In 3.5.1, it stopped working when another inline of the same parent, uses autocomplete_fields.

It will work, however, if I also add jquery in Media metaclass of the inline that uses autocomplete_fields.

Outside the admin there is no reason not to work. It is inside that things get complicated with autocomplete_fields of another inline.

@jpic
Copy link
Member

jpic commented Mar 20, 2020

I'm deeply sorry but actually in 3.5.0, it was still loading jquery despite the announce, it was fixed by e46300d which got in 3.5.1 release.

Ok so I think we have a documentation problem, but this is what I think should happen:

What we're trying to avoid: multiple jqueries loading in the same page, which creates inconsistencies such as : select2 loaded in one jquery, then another jquery is loaded and it doesn't have select2 anymore as such inlines won't work because they are created dynamically with the jquery that is loaded at this point in time.

Does that help ?

@raratiru
Copy link
Contributor Author

raratiru commented Mar 20, 2020

Yes, picking up django.JQuery and avoiding multiple jqueries in the admin is great, thank you!

However, (it should have been a cache mishap?) currently with 3.6.0.dev1:

  • Neither my hack (with the Media metaclass added to all inlines) works
  • Nor DAL out of the box (without external JQuery)

WithDALInline is dead. :-(


This is the relevant <script> part without external JQuery:

<script type="text/javascript" src="/admin/jsi18n/"></script>
<link href="/static/admin/css/vendor/select2/select2.css" type="text/css" media="screen" rel="stylesheet">
<link href="/static/vendor/select2/dist/css/select2.css" type="text/css" media="screen" rel="stylesheet">
<link href="/static/admin/css/autocomplete.css" type="text/css" media="screen" rel="stylesheet">
<link href="/static/autocomplete_light/select2.css" type="text/css" media="screen" rel="stylesheet">
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/select2/select2.full.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/select2.full.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/select2/i18n/en.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/i18n/en.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/inlines.js"></script>
<script type="text/javascript" src="/static/admin/js/autocomplete.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/forward.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/select2.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>
<script type="text/javascript" src="/static/admin/js/urlify.js"></script>
<script type="text/javascript" src="/static/admin/js/prepopulate.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/xregexp/xregexp.js"></script>

@jpic
Copy link
Member

jpic commented May 22, 2020

Right, I think the situation is still stuck here

@rogeriomgatto
Copy link

As a workaround, I got it working in the admin by adding the following Media class to my ModelAdmin class (django 2.2, dal 3.5.1). In my case, one admin worked (with a regular django autocomplete) and one didn't (without django autocomplete). I haven't had the time to find a minimal workaround.

    class Media:
        css = {
            'screen': [
                "vendor/select2/dist/css/select2.css",
                "admin/css/vendor/select2/select2.css",
                "admin/css/autocomplete.css",
                "autocomplete_light/select2.css",
            ]
        }
        js = [
            "admin/js/vendor/jquery/jquery.js",
            "autocomplete_light/jquery.init.js",
            "vendor/select2/dist/js/select2.full.js",
            "admin/js/vendor/select2/select2.full.js",
            "admin/js/jquery.init.js",
        ]

@raratiru
Copy link
Contributor Author

... ne admin worked (with a regular django autocomplete) and one didn't (without django autocomplete)

This solution makes everything work in my case and it uses inhouse jquery!

However, it breaks everything in DAL-3.6.0-dev1.

@mrts
Copy link

mrts commented Jun 7, 2020

As a workaround, I got it working in the admin by adding the following Media class to my ModelAdmin class (django 2.2, dal 3.5.1). In my case, one admin worked (with a regular django autocomplete) and one didn't (without django autocomplete). I haven't had the time to find a minimal workaround.

Thank you @rogeriomgatto ! This fixed jQuery loading in admin with Django 2.2.6 and DAL 3.5.1 for me as well.

@jpic
Copy link
Member

jpic commented Oct 12, 2020

We won't be affected by this with next AL release because we won't be using form media with the webcomponent.

In general, I'm leaving form.media, it's nice for little things for DAL it's been a disaster, using djwc for static instead, works much better and there's no conflict.

@raratiru
Copy link
Contributor Author

raratiru commented Oct 13, 2020

@jpic OK, thank you!

It was for a while that I missed what happened in this issue due to work overload...

Cheers to the next release, 3.8.1 is on as I see, I can't wait to see the results when I get home.

I am closing this issue in favor of #1168 .

@raratiru
Copy link
Contributor Author

raratiru commented Nov 20, 2020

@jpic As I am following your conversation with @orientalperil in #1168 , I entered the completely new world of front-end, SPAs, PWAs, web components (+ djwc ! ) and most of all, I re-tuned my thinking about javascript:

I realized that after 2015 this language has evolved into a really interesting OO programming language leaving behind the hacky bitter taste I thought it had. Wow!

I have just started learning Vue.js because it feels like it respects javascript and HTML as well (despite their attitude, svelte.js and svelteKit are also very promising), I got to meet snowpack and I feel excited.

Thank you for introducing me to this radically changing playground!

I decided to delve into Vue.js-3 using Vite which is created by Evan You along the lines of snowpack.

Anyway, these are just my first impressions I would like to share. Thank you again!

@jpic
Copy link
Member

jpic commented Nov 25, 2020 via email

@raratiru
Copy link
Contributor Author

@jpic I am still learning javascript and understanding what are those tools trying to do. I have not yet reached the step of integrating them with Django.

However, I understand that decoupling the front-end from the back-end, is the future.

I read somewhere that javascript has matured enough to be able to benefit from the horse power of lower level languages. The idea of a static html file able to dynamically transform itself in an effective and efficient way is just awesome.

In that scope, djwc and djsnowpack pave the way for a better... offline tomorrow!

I am excited.

@jpic
Copy link
Member

jpic commented Nov 26, 2020 via email

@raratiru
Copy link
Contributor Author

@jpic Thank you for the insight, I really see the big picture now! Indeed having one source of truth like django provides a huge development boost. It became very clear to me how I can merge the benefits and why, wow! -D

Thank you also for the crucial point of keeping the dbs synced when it comes to the offline feature. Actually I am planning to build an app and the data entry will be performed on the street by a handful of people that could be -for example- located in a basement.

crudlfa+ -> I just met this guy, nice tool, let me explore it more!

Α few seconds ago I saw this very interesting video by Evan You, explaining clearly, important details that are not obvious in the relevant pages of Vue-3 documentation. I am not sure I understand everything but the layout presented there made me feel that things evolve in a really interesting and mind blowing way!

I will surely stay tuned, thank you again!

@jpic
Copy link
Member

jpic commented Nov 27, 2020 via email

@raratiru
Copy link
Contributor Author

Good luck by all means!

The research I made those days enables me to understand that bringing web components into Python is a completely innovative change and I clearly see what makes Carlton Gibson (tough dude!) so excited about everything happening in #1168 ...

So yes, this is a solution!

Svelte has some syntactic idioms that deviate from javascript and I cannot easily spot them, yet. This is why I explored Vue.js hoping that the new tree-shaking compliance feature will help...

OK, it helps but there is a huge gap as I see in that article!

The web changes and Django community wins brave fights in the very front line. This is not only exciting but also soothing and relieving. Thank you for being here and sharing!

All these "details" were like a little "black spot" I avoided in my daily schedule. Thanks to COVID, I approached it and !OH! it is not a spot rather a whole new world... happening now!

@jpic
Copy link
Member

jpic commented Dec 3, 2020 via email

@raratiru
Copy link
Contributor Author

raratiru commented Dec 4, 2020

@jpic Thank you * N! pytest-bdd with pytest-splinter will form an amazing tool to drive TDD.

I am just recompiling my head based on the configuration provided by this thorough presentation of Python 2009 yield.

I lived a "close to killer joke" extra-terrestrial experience regarding the power of Python when combined with a free as in Freedom mental setup.

Many things have changed since then, but yes, I will definitely be using Python as the basis of my projects and I would locally plug the most suited tools if a task requires so, I primarily regard this as an exercise to strengthen my imagination.

Transcrypt seems to be rocking, and your intuitive tests are awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants