我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.forms.models.ModelChoiceIterator()。
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)
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)
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
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')
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)