# Path: mutant/utils.py
# def choices_from_dict(choices):
#     for grp, choices in groupby(choices, key=group_item_getter):
#         if grp is None:
#             for choice in choices:
#                 yield (choice['value'], choice['label'])
#         else:
#             yield (grp, tuple(
#                 (choice['value'], choice['label']) for choice in choices)
#             )


from django import forms
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_text
from django.utils.functional import LazyObject
from .utils import choices_from_dict
            from mutant.models import FieldDefinition
        models = self.__dict__.get('models')
        self._wrapped = queryset.filter(
            pk__in=[ct.pk for ct in ContentType.objects.get_for_models(
                *models, for_concrete_models=False
            ).values()]
        )


class LazyFieldDefinitionGroupedChoices(LazyObject):
    def __init__(self, queryset, empty_label, label_from_instance):
        super(LazyFieldDefinitionGroupedChoices, self).__init__()
        self.__dict__.update(
            queryset=queryset, empty_label=empty_label,
            label_from_instance=label_from_instance
        )

    def _setup(self):
        queryset = self.__dict__.get('queryset')
        label_from_instance = self.__dict__.get('label_from_instance')
        empty_label = self.__dict__.get('empty_label')
        definition_choices = []
        for content_type in queryset:
            definition = content_type.model_class()
            category = definition.get_field_category()
            definition_choices.append({
                'group': smart_text(category) if category else None,
                'value': content_type.pk,
                'label': label_from_instance(content_type),
            })
        choices = list(

# Path: mutant/utils.py
# def choices_from_dict(choices):
#     for grp, choices in groupby(choices, key=group_item_getter):
#         if grp is None:
#             for choice in choices:
#                 yield (choice['value'], choice['label'])
#         else:
#             yield (grp, tuple(
#                 (choice['value'], choice['label']) for choice in choices)
#             )


# Path: mutant/forms.py
from django import forms
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_text
from django.utils.functional import LazyObject
from .utils import choices_from_dict
            from mutant.models import FieldDefinition

        models = self.__dict__.get('models')
        self._wrapped = queryset.filter(
            pk__in=[ct.pk for ct in ContentType.objects.get_for_models(
                *models, for_concrete_models=False
            ).values()]
        )


class LazyFieldDefinitionGroupedChoices(LazyObject):
    def __init__(self, queryset, empty_label, label_from_instance):
        super(LazyFieldDefinitionGroupedChoices, self).__init__()
        self.__dict__.update(
            queryset=queryset, empty_label=empty_label,
            label_from_instance=label_from_instance
        )

    def _setup(self):
        queryset = self.__dict__.get('queryset')
        label_from_instance = self.__dict__.get('label_from_instance')
        empty_label = self.__dict__.get('empty_label')
        definition_choices = []
        for content_type in queryset:
            definition = content_type.model_class()
            category = definition.get_field_category()
            definition_choices.append({
                'group': smart_text(category) if category else None,
                'value': content_type.pk,
                'label': label_from_instance(content_type),
            })
        choices = list(

            choices_from_dict(