X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fprojects%2Fxerxes.git;a=blobdiff_plain;f=tools%2Fmisc.py;fp=tools%2Fmisc.py;h=ddc6cee7490dc6fc3492376f5e004821827b2952;hp=2c785482e5a9e1da735a4d05c3b75ec41b78493b;hb=381c81a8ffac8c4b3abf6a762d031a7e261e8eaa;hpb=dad2574dc3f0cc1d114a85ac44ef440b5baeeaa8 diff --git a/tools/misc.py b/tools/misc.py index 2c78548..ddc6cee 100644 --- a/tools/misc.py +++ b/tools/misc.py @@ -40,3 +40,20 @@ def make_choices(objects): FormField and is not public. """ return [(o.pk, o) for o in objects] + +def filter_choices(choices, filter): + """ + Returns the given choices list with only the choices with the names + in filter left. For example, when a model defines + A = 'A' + B = 'B' + FOO_CHOICES = ( + (A, "Foo A"), + (B, "Foo B") + ) + + you can later define a modelfield using + + foo = ChoiceField(choices=filter_choices(Foo.FOO_CHOICES, [Foo.A])) + """ + return [(name, value) for (name, value) in choices if name in filter]