Python django.forms.models.ModelChoiceIterator() Examples
The following are 10
code examples of django.forms.models.ModelChoiceIterator().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.forms.models
, or try the search function
.
Example #1
Source File: forms.py From mendelmd with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #2
Source File: forms.py From mendelmd with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #3
Source File: forms.py From mendelmd with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #4
Source File: test_modelchoicefield.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #5
Source File: test_modelchoicefield.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #6
Source File: relations.py From esdc-ce with Apache License 2.0 | 6 votes |
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
Example #7
Source File: select2.py From online-judge with GNU Affero General Public License v3.0 | 5 votes |
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
Example #8
Source File: test_modelchoicefield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
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), )
Example #9
Source File: test_modelchoicefield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
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), )
Example #10
Source File: relations.py From esdc-ce with Apache License 2.0 | 5 votes |
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)