本文整理匯總了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()]
)