本文整理汇总了Python中django.db.models.BLANK_CHOICE_DASH属性的典型用法代码示例。如果您正苦于以下问题:Python models.BLANK_CHOICE_DASH属性的具体用法?Python models.BLANK_CHOICE_DASH怎么用?Python models.BLANK_CHOICE_DASH使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类django.db.models
的用法示例。
在下文中一共展示了models.BLANK_CHOICE_DASH属性的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_choices
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import BLANK_CHOICE_DASH [as 别名]
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
示例2: test_create_choices_objects_exist
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import BLANK_CHOICE_DASH [as 别名]
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)
示例3: render_option
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import BLANK_CHOICE_DASH [as 别名]
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)
}
示例4: test_create_choices_objects_do_not_exist
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import BLANK_CHOICE_DASH [as 别名]
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)
示例5: get_field_kwargs
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import BLANK_CHOICE_DASH [as 别名]
def get_field_kwargs(self, struct_value):
kwargs = super().get_field_kwargs(struct_value)
kwargs['choices'].insert(0, BLANK_CHOICE_DASH[0])
return kwargs
示例6: __init__
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import BLANK_CHOICE_DASH [as 别名]
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()]
)