Opened 11 years ago

Closed 7 years ago

#21318 closed Cleanup/optimization (duplicate)

Clarify the ordering of the various Media classes

Reported by: leandro.gz73@… Owned by: nobody
Component: Documentation Version: dev
Severity: Normal Keywords: admin, js, jquery
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm using django 1.5.4

I've got a custom user model, with ManyToManyField called 'favorite_bands'.
I'd like to use http://ivaynberg.github.io/select2/ widget.
This includes some js files, but I need to include a jquery library higher than 1.4.
If I user the MEDIA class https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-asset-definitions, this do not ensure me the needed order (first jquery.1.10, then anothers).

I've done this: http://stackoverflow.com/questions/1164358/django-admin-custom-javascript-load-order

Can you add a 'pre_media' var in AdminModel to set a user ordered list of js and render them before 'media' var? Or add it to MEDIA class?
That could be a solution.

This is the relevant part of code

{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
<script type="text/javascript" src="{{ STATIC_URL }}js/jquery/jquery-1.10.1.js"></script>
{# I've inserted the jquery version, I'd like to have a var 'pre_media' var here #}
{{ media }}
{% endblock %}
class UserChangeForm(forms.ModelForm):

    class Meta:
        model = get_user_model()
        widgets = {
            'favorite_bands': HeavySelect2MultipleWidget(
                data_view='search_bands', select2_options={
                    'width': 600,
                    'closeOnSelect': True
                }),         
        }

Change History (8)

comment:1 by Marc Tamlyn, 11 years ago

Personally, my feeling is that you need to override the template of you want to do something clever like this. I don't think this is a helpful feature to add.

I'm also a little confused why you're trying to insert a different jquery - the admin in 1.5.x uses jq 1.10 which is newer than 1.4 - so select2 should work fine.

comment:2 by Tim Graham, 11 years ago

Marc, I'm not sure if you're correct re: the version of jQuery the Django includes. According to the docs: "Changed in Django 1.6: The embedded jQuery has been upgraded from 1.4.2 to 1.9.1."

However, I agree that overriding the template is probably the way to go here, unless there's a very clean way to do this in code. I don't think we should bent over backwards to meet this use case.

comment:3 by lalo, 11 years ago

Yes? overriding the template? copying and pasting all these code?
I don't belive so.

What I'd like it's a way to ensure the order in the js.
In this case I need jquery before select2 libraries, but if want to include another after or before(maybe a internationalization library, how do it?
maybe the ModelAdmin.MEDIA precedes the Form.MEDIAs or Widget.MEDIA, I think.

comment:4 by Tim Graham, 11 years ago

Component: contrib.adminDocumentation
Summary: Insert jquery before another js libraries.Clarify the ordering of the various Media classes
Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization
Version: 1.5master

I haven't used Media very much, but it seems like what you are trying to do should be possible without additional features. There must be a defined order in how the various Media classes you described are combined and output. Perhaps we can improve our documentation in this area.

comment:5 by lalo, 11 years ago

Ok, if you can ensure the Media order, this could be the solution. But now, there isn't documentation or a clear way to do it.

Thank for you time.

comment:6 by Jonathan Potter, 10 years ago

When rendering a form, the Media classes seem to be rendered to the template in the order of the fields they are attached to. I couldn't find this documented anywhere (maybe it should be) but at least in the admin interface this seems to be the case.

This doesn't really help you order them because you don't usually want to change the order of your fields but at least it's a start to know how things will be ordered.

comment:7 by Tim Graham, 9 years ago

See also #12265, "Media (js/css) collection strategy in Forms has no order dependence concept".

comment:8 by Collin Anderson, 7 years ago

Resolution: duplicate
Status: newclosed

I believe this is fixed by #28377

Note: See TracTickets for help on using tickets.
Back to Top