Python django.db.models.BLANK_CHOICE_DASH Examples

The following are 6 code examples of django.db.models.BLANK_CHOICE_DASH(). 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.db.models , or try the search function .
Example #1
Source File: widgets.py    From django-content-gallery with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _create_choices(self):
        """
        Adds objects of the selected model to the choices list.
        """
        # copy the empty choice
        # we shouldn't add choices to original list
        # also in that case BLANK_CHOICE_DASH would keep
        # all previously added choices
        choices = copy.copy(BLANK_CHOICE_DASH)
        # add objects only if the model class specified
        if self.model_class:
            items = self.model_class.objects.all()
            for item in items:
                choices.append((str(item.id), item))
        # replace original choices
        self.choices = choices 
Example #2
Source File: test_widgets.py    From django-content-gallery with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_create_choices_objects_exist(self):
        """
        Checks whether the _create_choices method creates choices for
        all objects of the selected model if objects exist. Also the list
        should include an empty choice.
        """
        # set selected model class with existing objects
        self.widget.model_class = TestModel
        # call the _create_choices method
        widgets.ObjectIdSelect._create_choices(self.widget)
        # check whether the list contains an empty choice
        self.assertIn(BLANK_CHOICE_DASH[0], self.widget.choices)
        # create choices
        choice1 = (str(self.object1.pk), self.object1)
        choice2 = (str(self.object2.pk), self.object2)
        # check whether the list contains both TestModel objects
        self.assertIn(choice1, self.widget.choices)
        self.assertIn(choice2, self.widget.choices)
        # check whether there are 3 choices so the list contains nothing
        # but two objects of the TestModel and an empty choice
        self.assertEqual(len(self.widget.choices), 3) 
Example #3
Source File: widgets.py    From oldp with MIT License 6 votes vote down vote up
def render_option(self, name, selected_choices,
                      option_value, option_label):
        option_value = force_text(option_value)
        if option_label == BLANK_CHOICE_DASH[0][1]:
            option_label = _("All")
        data = self.data.copy()
        data[name] = option_value
        selected = data == self.data or option_value in selected_choices
        try:
            url = data.urlencode()
        except AttributeError:
            url = urlencode(data)
        return self.option_string(selected) % {
            'attrs': selected and ' class="selected"' or '',
            'query_string': url,
            'name': name,
            'value': option_value,
            'label': force_text(option_label)
        } 
Example #4
Source File: test_widgets.py    From django-content-gallery with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_create_choices_objects_do_not_exist(self):
        """
        Checks whether the _create_choices method creates an empty choice
        only if there is no objects of the selected model
        """
        # set selected model class without existing objects
        self.widget.model_class = AnotherTestModel
        # call the _create_choices method
        widgets.ObjectIdSelect._create_choices(self.widget)
        # check whether the list contains only one choice
        self.assertEqual(len(self.widget.choices), 1)
        # check whether an empty choice presents in the list
        self.assertIn(BLANK_CHOICE_DASH[0], self.widget.choices) 
Example #5
Source File: blocks.py    From hypha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_field_kwargs(self, struct_value):
        kwargs = super().get_field_kwargs(struct_value)
        kwargs['choices'].insert(0, BLANK_CHOICE_DASH[0])
        return kwargs 
Example #6
Source File: admin.py    From esdc-ce with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(DomainAdminForm, self).__init__(*args, **kwargs)
        self.fields['master'].required = False
        self.fields['user'].required = False
        self.fields['user'].widget = forms.Select(
            choices=BLANK_CHOICE_DASH + [(i.id, i.username) for i in User.objects.all()]
        )