From 74cf9d437ac242f610b17d388d3728b36afb4f64 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 3 Feb 2009 23:09:08 +0100 Subject: [PATCH] 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. --- tools/misc.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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] -- 2.30.2