X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/7902f12cf4e054ee4dae904687ac97d5e2f7360a..37f95f6fd3e1da107a0eaf28c516e71206100bf4:/philo/models/fields/__init__.py?ds=sidebyside diff --git a/philo/models/fields/__init__.py b/philo/models/fields/__init__.py index 0003575..575b3a4 100644 --- a/philo/models/fields/__init__.py +++ b/philo/models/fields/__init__.py @@ -7,6 +7,7 @@ from django.utils.text import capfirst from django.utils.translation import ugettext_lazy as _ from philo.forms.fields import JSONFormField +from philo.utils.registry import RegistryIterator from philo.validators import TemplateValidator, json_validator from philo.forms.widgets import EmbedWidget #from philo.models.fields.entities import * @@ -77,7 +78,7 @@ class JSONField(models.TextField): class SlugMultipleChoiceField(models.Field): - """Stores a selection of multiple items with unique slugs in the form of a comma-separated list.""" + """Stores a selection of multiple items with unique slugs in the form of a comma-separated list. Also knows how to correctly handle :class:`RegistryIterator`\ s passed in as choices.""" __metaclass__ = models.SubfieldBase description = _("Comma-separated slug field") @@ -133,6 +134,16 @@ class SlugMultipleChoiceField(models.Field): if invalid_values: # should really make a custom message. raise ValidationError(self.error_messages['invalid_choice'] % invalid_values) + + def _get_choices(self): + if isinstance(self._choices, RegistryIterator): + return self._choices.copy() + elif hasattr(self._choices, 'next'): + choices, self._choices = itertools.tee(self._choices) + return choices + else: + return self._choices + choices = property(_get_choices) try: