public
Created

  • Download Gist
choosey.js
JavaScript
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
(function($) {
var $groups, $empty;
 
function init() {
var $el = $("#id_number");
$empty = $el.find('option:first');
$groups = $el.find('optgroup');
 
$('#id_letter').change(update);
update();
}
 
function update() {
/* Select the option group with the correct label, and enable it. Then,
* select the remaining option groups and disable them. */
var selector = '[label="' + $(this).val() + '"]',
$disabled;
 
$groups.filter(selector).attr('disabled', false);
$disabled = $groups.not(selector).attr('disabled', true);
 
if ($disabled.find(':selected').length > 0) {
$empty.attr('selected', true);
}
}
 
$(init);
}(django.jQuery || window.jQuery));
models.py
Python
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
from django.contrib import admin
from django.db import models
 
 
class Example(models.Model):
""" Let's use a dictionary to reference which numbers belong to a specific
letter choice.
"""
CHOICES = (
('A', ((1, '1'), (2, '2'), (3, '3'))),
('B', ((4, '4'), (5, '5'), (6, '6'))),
)
 
# Just the letters: (('A', 'A'), ('B', 'B'))
LETTER_CHOICES = tuple((l[0], l[0]) for l in CHOICES)
 
letter = models.CharField(max_length=1, choices=LETTER_CHOICES)
number = models.PositiveIntegerField(max_length=1, choices=CHOICES)
 
 
class ExampleAdmin(admin.ModelAdmin):
""" Use our custom form in the admin. """
class Media:
""" Include the javascript. """
js = ('choosey.js',) # This is prepended with your STATIC_URL
admin.site.register(Example, ExampleAdmin)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.