python类ModelChoiceIterator()的实例源码

forms.py 文件源码 项目:mendelmd 作者: raonyguimaraes 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
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)
forms.py 文件源码 项目:djangocms-association 作者: python-spain 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def render_options(self, *args):
        """Render only selected options and set QuerySet from :class:`ModelChoicesIterator`."""
        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):
            # Mejora significativa de rendimiernto con esta condición
            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)
relations.py 文件源码 项目:esdc-ce 作者: erigones 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
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
widgets.py 文件源码 项目:tumanov_castleoaks 作者: Roamdev 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def choices(self, value):
        """ ??????????? ??????, ?? ??????? ???????????? ??????? """
        self._choices = value
        if isinstance(value, ModelChoiceIterator):
            self.app_label = value.queryset.model._meta.app_label
            self.model_name = value.queryset.model._meta.model_name
        else:
            raise TypeError('choices for AutocompleteWidget must be an instance of ModelChoiceIterator')
relations.py 文件源码 项目:esdc-ce 作者: erigones 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)


问题


面经


文章

微信
公众号

扫码关注公众号