From: Matthijs Kooijman Date: Tue, 3 Feb 2009 22:09:08 +0000 (+0100) Subject: Add make_choices function. X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fprojects%2Fxerxes.git;a=commitdiff_plain;h=74cf9d437ac242f610b17d388d3728b36afb4f64 Add make_choices function. This function can transform a list of Model objects into a list suitable for use as a choices list in fields and widgets. --- diff --git a/tools/misc.py b/tools/misc.py index ba84f05..2c78548 100644 --- a/tools/misc.py +++ b/tools/misc.py @@ -28,3 +28,15 @@ def log_error(func): traceback.print_exc() raise e return show + +def make_choices(objects): + """ + Transforms a list (or iteratable) of model objects to a list + suitable to be used as a list of choices in form widgets like + Select. + + This fullfills a similar (but simpler) function as + django.forms.models.ModelChoiceIterator, but that one requires a + FormField and is not public. + """ + return [(o.pk, o) for o in objects]