当前位置: 首页>>代码示例>>Python>>正文


Python models.ModelChoiceIterator方法代码示例

本文整理汇总了Python中django.forms.models.ModelChoiceIterator方法的典型用法代码示例。如果您正苦于以下问题:Python models.ModelChoiceIterator方法的具体用法?Python models.ModelChoiceIterator怎么用?Python models.ModelChoiceIterator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms.models的用法示例。


在下文中一共展示了models.ModelChoiceIterator方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: render_options

# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelChoiceIterator [as 别名]
def render_options(self, *args):
        """Render only selected options and set QuerySet from :class:`ModelChoiceIterator`."""
        try:
            selected_choices, = args
        except ValueError:
            choices, selected_choices = args
            choices = chain(self.choices, choices)
        else:
            choices = self.choices
        selected_choices = {force_text(v) for v in selected_choices}
        output = ['<option></option>' if not self.is_required and not self.allow_multiple_selected else '']
        if isinstance(self.choices, ModelChoiceIterator):
            if self.queryset is None:
                self.queryset = self.choices.queryset
            selected_choices = {c for c in selected_choices
                                if c not in self.choices.field.empty_values}
            choices = [(obj.pk, self.label_from_instance(obj))
                       for obj in self.choices.queryset.filter(pk__in=selected_choices)]
        else:
            choices = [(k, v) for k, v in choices if force_text(k) in selected_choices]
        for option_value, option_label in choices:
            output.append(self.render_option(selected_choices, option_value, option_label))
        return '\n'.join(output) 
开发者ID:raonyguimaraes,项目名称:mendelmd,代码行数:25,代码来源:forms.py

示例2: test_overridable_choice_iterator

# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelChoiceIterator [as 别名]
def test_overridable_choice_iterator(self):
        """
        Iterator defaults to ModelChoiceIterator and can be overridden with
        the iterator attribute on a ModelChoiceField subclass.
        """
        field = forms.ModelChoiceField(Category.objects.all())
        self.assertIsInstance(field.choices, ModelChoiceIterator)

        class CustomModelChoiceIterator(ModelChoiceIterator):
            pass

        class CustomModelChoiceField(forms.ModelChoiceField):
            iterator = CustomModelChoiceIterator

        field = CustomModelChoiceField(Category.objects.all())
        self.assertIsInstance(field.choices, CustomModelChoiceIterator) 
开发者ID:nesdis,项目名称:djongo,代码行数:18,代码来源:test_modelchoicefield.py

示例3: __init__

# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelChoiceIterator [as 别名]
def __init__(self, *args, **kwargs):
        queryset = kwargs.pop('queryset', None)
        self.many = kwargs.pop('many', self.many)
        if self.many:
            self.widget = self.many_widget
            self.form_field_class = self.many_form_field_class

        kwargs['read_only'] = kwargs.pop('read_only', self.read_only)
        super(RelatedField, self).__init__(*args, **kwargs)

        if not self.required:
            # Accessed in ModelChoiceIterator django/forms/models.py:1034
            # If set adds empty choice.
            self.empty_label = BLANK_CHOICE_DASH[0][1]

        self.queryset = queryset 
开发者ID:erigones,项目名称:esdc-ce,代码行数:18,代码来源:relations.py

示例4: format_value

# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelChoiceIterator [as 别名]
def format_value(self, value):
        result = super(HeavySelect2Mixin, self).format_value(value)
        if isinstance(self.choices, ModelChoiceIterator):
            chosen = copy(self.choices)
            chosen.queryset = chosen.queryset.filter(pk__in=[
                int(i) for i in result if isinstance(i, int) or i.isdigit()
            ])
            self.choices = set(chosen)
        return result 
开发者ID:DMOJ,项目名称:online-judge,代码行数:11,代码来源:select2.py

示例5: test_choice_iterator_passes_model_to_widget

# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelChoiceIterator [as 别名]
def test_choice_iterator_passes_model_to_widget(self):
        class CustomModelChoiceValue:
            def __init__(self, value, obj):
                self.value = value
                self.obj = obj

            def __str__(self):
                return str(self.value)

        class CustomModelChoiceIterator(ModelChoiceIterator):
            def choice(self, obj):
                value, label = super().choice(obj)
                return CustomModelChoiceValue(value, obj), label

        class CustomCheckboxSelectMultiple(CheckboxSelectMultiple):
            def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
                option = super().create_option(name, value, label, selected, index, subindex=None, attrs=None)
                # Modify the HTML based on the object being rendered.
                c = value.obj
                option['attrs']['data-slug'] = c.slug
                return option

        class CustomModelMultipleChoiceField(forms.ModelMultipleChoiceField):
            iterator = CustomModelChoiceIterator
            widget = CustomCheckboxSelectMultiple

        field = CustomModelMultipleChoiceField(Category.objects.all())
        self.assertHTMLEqual(
            field.widget.render('name', []),
            '''<ul>
<li><label><input type="checkbox" name="name" value="%d" data-slug="entertainment">Entertainment</label></li>
<li><label><input type="checkbox" name="name" value="%d" data-slug="test">A test</label></li>
<li><label><input type="checkbox" name="name" value="%d" data-slug="third-test">Third</label></li>
</ul>''' % (self.c1.pk, self.c2.pk, self.c3.pk),
        ) 
开发者ID:nesdis,项目名称:djongo,代码行数:37,代码来源:test_modelchoicefield.py

示例6: _get_choices

# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelChoiceIterator [as 别名]
def _get_choices(self):
        # If self._choices is set, then somebody must have manually set
        # the property self.choices. In this case, just return self._choices.
        if hasattr(self, '_choices'):
            return self._choices

        # Otherwise, execute the QuerySet in self.queryset to determine the
        # choices dynamically. Return a fresh ModelChoiceIterator that has not been
        # consumed. Note that we're instantiating a new ModelChoiceIterator *each*
        # time _get_choices() is called (and, thus, each time self.choices is
        # accessed) so that we can ensure the QuerySet has not been consumed. This
        # construct might look complicated but it allows for lazy evaluation of
        # the queryset.
        return ModelChoiceIterator(self) 
开发者ID:erigones,项目名称:esdc-ce,代码行数:16,代码来源:relations.py


注:本文中的django.forms.models.ModelChoiceIterator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。