Login

Django using admin horizontal filter in forms

Author:
crodjer
Posted:
June 20, 2011
Language:
Python
Version:
Not specified
Score:
1 (after 1 ratings)

This snippet uses the admin FilterSelectMultiple widget in normal forms.

Earlier I tried this without the internationalization javascript and failed, so I looked around the web and found couple of posts, they worked but suggest many customizations.
I learnt from them that they had this jsi18n javascript included. I included it with mine and it worked.

I have written a detailed post about this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# forms.py
from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple

class TagForm(forms.Form):
    tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),
                                          label=_('Select tags'),
                                          required=False,
                                          widget=FilteredSelectMultiple(
                                                    _('tags'),
                                                    False,
                                                 ))

    class Media:
        css = {
            'all':['admin/css/widgets.css',
                   'css/uid-manage-form.css'],
        }
        # Adding this javascript is crucial
        js = ['/admin/jsi18n/']

#######################
# template
<!--HEAD -->
{{ form.media }}
<!--BODY-->
{{ form }}

#######################
# extra css

.selector{
    float:none;
    display:block;
    height:330px;
}

.selector input[type="submit"]{
    display:block;
    clear:both;
}

.selector h2{
    font-size:15px;
}
.selector  select{
    margin: 2px 0;
    padding: 2px 3px;
    font-weight: normal;
    width: 270px !important;
    height: 17.2em;
}

textarea{
    display:block;
    width:450px;
}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.