Opened 19 months ago

Last modified 4 months ago

#21318 new Cleanup/optimization

Clarify the ordering of the various Media classes

Reported by: leandro.gz73@… Owned by: nobody
Component: Documentation Version: master
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 (6)

comment:1 Changed 19 months ago by mjtamlyn

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 19 months ago by timo

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 Changed 19 months ago by lalo

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 Changed 19 months ago by timo

  • Component changed from contrib.admin to Documentation
  • Summary changed from Insert jquery before another js libraries. to Clarify the ordering of the various Media classes
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from New feature to Cleanup/optimization
  • Version changed from 1.5 to master

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 Changed 19 months ago by lalo

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 Changed 4 months ago by jpotterm

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.

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