Search code examples
djangopython-2.7widgetdjango-1.10

FilteredSelectMultiple widget not working in 1.10.1 (Works in 1.8)


I am using a FilteredSelectMultiple widget from django.contrib.admin.widgets for a ModelMultipleChoiceField. This works great in my django 1.8 environment, but when I try it with 1.10.1, it fails to render the addEvent <script> tag after the select.

from forms.py

class UserCatForm(forms.ModelForm):

    """Form to select Categories for user"""

    form_errors = {"required": "You must choose at least one expense category"}
    cats = forms.ModelMultipleChoiceField(
        error_messages=form_errors,
        label="",
        queryset=Categories.objects.all(),
        widget=FilteredSelectMultiple("Categories", is_stacked=False, attrs={'class':'form-control'}),
        )

    class Meta:
        name = 'UserCatForm'
        model = UserCat
        fields = "__all__"
        widgets = {'user':forms.HiddenInput(),}

base.html

{% load staticfiles %}
<html>
<head>
 {% load bootstrap3 %}
 {% bootstrap_css %}
 {% bootstrap_messages %}

 <link rel="stylesheet" href="{% static 'css/budget.css' %}">
 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
 <link rel="stylesheet" type="text/css" href="/static/admin/css/widgets.css" />

 <script type="text/javascript" src="{% static 'js/jsi18n.js' %}"></script>
 <script type="text/javascript" src="/static/admin/js/jquery.min.js"></script>
 <script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
 <script type="text/javascript" src="{% static 'admin/js/core.js' %}"></script>
 <script type="text/javascript" src="{% static 'admin/js/SelectBox.js' %}"></script>
 <script type="text/javascript" src="{% static 'admin/js/SelectFilter2.js' %}"></script>
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

 <script>
 $(function() {
     $( "#id_nextDueDate" ).datepicker();
     $( "#id_endDate" ).datepicker();
 });
 </script>


{% block title %}{% endblock %}
</head>
<div class="container-fluid">
  <div class="row">
    <div class="col-xs-12 well well-sm">
     <h1 class="center">GreenPrint PayPlanner</h1>
      <div class="row">
      <div class="col-xs-6">
      {% if user.is_authenticated %}
       {% if user.get_full_name %}
       <p>Welcome {{ user.first_name }}!
       {% else %}
       <p>Welcome {{ user }}!
       {% endif %}
        <a title="Manage Settings" href="{% url 'manage' %}"><img border="0" width="75" alt="Manage Account" src="{% static "pics/settings.png" %}"></a>
       </p>
       </div>
       <div class="col-xs-6 text-right">
       <a href="{% url 'logout' %}" class="text-warning">Logout</a>
       </div>
      {% endif %}
      </div>
    </div>
  </div>
 <div class="row">
   <div class="col-xs-12">{% block content %}{% endblock %}</div>
  </div>
  <div class="row">
   <div class="col-xs-12">{% block footer %}{% endblock %}</div>
  </div>
 </div>
</html>

manage.html - template with form

{% extends "base.html" %}
{% load bootstrap3 %}

{% block title %}
<title>Account Settings</title>
{% endblock %}

{% block content %}
<div class="container-fluid">
 <div class="row">
  <div class="col-m-3"></div>
  <div class="col-m-9">
   <form action="{% url 'manage' %}" method="post">
    <div class="form-group">
     {% csrf_token %}
     {{ form.as_table }}
    </div>
  </div>
  <div class="col-m-3"></div>
 </div>
 <div class="row center">
  <div class="col-xs-12">
   {% if is_get %}
   <div class="center btn-group-vertical btn-group-lg">
    <input type="submit" name="profile" value="Edit Profile" class="btn btn-primary"/>
    <input type="submit" name="categories" value="Edit Categories" class="btn btn-primary"/>
    <input type="submit" name="deldata" value="Delete Budget" class="btn btn-warning"/>
    <input type="submit" name="delacct" value="Delete Account" class="btn btn-danger"/>
    <input type="submit" name="home" value="Home" class="btn btn-default"/>
   </div>
   {% else %}
    <div class="btn-group">
    <input type="submit" name="save_{{ form.Meta.name }}" value="Save" class="btn btn-primary"/>
    <input type="submit" name="cancel" value="Cancel" class="btn btn-default"/>
   </div>
   {% endif %}
  </div>
 </div>
</form>
</div>
{% endblock %}
{% block footer %}
<div class="center">{{ footer }}</div>
{% endblock %}

Working page source form:

<tr><th></th><td><select multiple="multiple" class="selectfilter" id="id_cats" name="cats">
<option value="1" selected="selected">Other</option>
<option value="2" selected="selected">Education</option>
<option value="3" selected="selected">Utilities</option>
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_cats", "Categories", 0, "/static/admin/"); });</script>
<input id="id_user" name="user" type="hidden" value="1" /></td></tr>

Not Working page source:

<tr><th></th><td><select multiple="multiple" class="selectfilter" data-field-name="Categories" data-is-stacked="0" id="id_cats" name="cats" required>
<option value="1" selected="selected">Other</option>
<option value="2" selected="selected">Utilities</option>
<option value="3" selected="selected">Education</option>
<option value="4" selected="selected">Car</option>
</select><input id="id_user" name="user" type="hidden" value="1" /></td></tr>

Solution

  • I have found a more django-esk way of fixing this, figured I would add it if anyone comes across this issue. This is working in both 1.8 and 1.10.1

    I put the addEvent call into an external js file ./static/js/catsfilter.js

    addEvent(window, "load", function(e) {SelectFilter.init("id_cats", "Categories", 0, "/static/admin/"); });
    

    Created a Media subclass on my form to call the js

    class Media:
            js = ("js/catsfilter.js",)
    

    And removed the call to core.js, SelectBox.js and SelectFilter2, replaced with a call to the forms media in my base.html head section

     <script type="text/javascript" src="{% static 'js/jsi18n.js' %}"></script>
     <script type="text/javascript" src="/static/admin/js/jquery.min.js"></script>
     <script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
        {{ form.media }}