本文整理汇总了Python中django.forms.TypedChoiceField方法的典型用法代码示例。如果您正苦于以下问题:Python forms.TypedChoiceField方法的具体用法?Python forms.TypedChoiceField怎么用?Python forms.TypedChoiceField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms
的用法示例。
在下文中一共展示了forms.TypedChoiceField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def __init__(self, league, player, *args, **kwargs):
super(NotificationsForm, self).__init__(*args, **kwargs)
for type_, _ in PLAYER_NOTIFICATION_TYPES:
setting = PlayerNotificationSetting.get_or_default(player=player, league=league,
type=type_)
self.fields[type_ + "_lichess"] = forms.BooleanField(required=False, label="Lichess",
initial=setting.enable_lichess_mail)
self.fields[type_ + "_slack"] = forms.BooleanField(required=False, label="Slack",
initial=setting.enable_slack_im)
self.fields[type_ + "_slack_wo"] = forms.BooleanField(required=False,
label="Slack (with opponent)",
initial=setting.enable_slack_mpim)
if type_ == 'before_game_time':
offset_options = [(5, '5 minutes'), (10, '10 minutes'), (20, '20 minutes'),
(30, '30 minutes'), (60, '1 hour'), (120, '2 hours')]
self.fields[type_ + '_offset'] = forms.TypedChoiceField(choices=offset_options,
initial=int(
setting.offset.total_seconds()) / 60,
coerce=int)
示例2: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def formfield(self, **kwargs):
class SuggestiveTypedChoiceFormField(forms.TypedChoiceField):
widget = TextWithDatalistInput
def to_python(self, value):
value = super().to_python(value)
if value not in self.empty_values:
try:
value = next(i for (i, choice) in self.choices if choice == value)
except StopIteration:
pass
return value
def validate(self, value):
_handle_invalid_choice(self, value)
defaults = {'choices_form_class': SuggestiveTypedChoiceFormField}
defaults.update(kwargs)
return super().formfield(**defaults)
示例3: set_fields
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def set_fields(cls, category, products):
choices = []
for product in products:
choice_text = "%s -- $%d" % (product.name, product.price)
choices.append((product.id, choice_text))
if not category.required:
choices.append((0, "No selection"))
cls.base_fields[cls.FIELD] = forms.TypedChoiceField(
label=category.name,
widget=forms.RadioSelect,
choices=choices,
empty_value=0,
coerce=int,
)
示例4: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def formfield(self, form_class=None, choices_form_class=None, **kwargs):
"""
Returns a django.forms.Field instance for this database Field.
"""
defaults = {'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text}
if self.has_default():
if callable(self.default):
defaults['initial'] = self.default
defaults['show_hidden_initial'] = True
else:
defaults['initial'] = self.get_default()
if self.choices:
# Fields with choices get special treatment.
include_blank = (self.blank or
not (self.has_default() or 'initial' in kwargs))
defaults['choices'] = self.get_choices(include_blank=include_blank)
defaults['coerce'] = self.to_python
if self.null:
defaults['empty_value'] = None
if choices_form_class is not None:
form_class = choices_form_class
else:
form_class = forms.TypedChoiceField
# Many of the subclass-specific formfield arguments (min_value,
# max_value) don't apply for choice fields, so be sure to only pass
# the values that TypedChoiceField will understand.
for k in list(kwargs):
if k not in ('coerce', 'empty_value', 'choices', 'required',
'widget', 'label', 'initial', 'help_text',
'error_messages', 'show_hidden_initial'):
del kwargs[k]
defaults.update(kwargs)
if form_class is None:
form_class = forms.CharField
return form_class(**defaults)
示例5: get_responsible_form
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def get_responsible_form(field_name, request, issue, data=None, *args, **kwargs):
class _form(DefaultForm):
responsible_name = forms.TypedChoiceField(get_users_choise(issue, 'responsible'), coerce=user_id2user, label='',
required=False)
return _form(field_name, request, issue, data, *args, **kwargs)
示例6: get_status_form
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def get_status_form(field_name, request, issue, data=None, *args, **kwargs):
class _form(DefaultForm):
lang = request.user.profile.language
status = forms.TypedChoiceField(get_status_choice(issue, lang),
coerce=status_id2status, label='', required=False)
return _form(field_name, request, issue, data, *args, **kwargs)
示例7: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def formfield(self, form_class=None, choices_form_class=None, **kwargs):
"""Return a django.forms.Field instance for this field."""
defaults = {'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text}
if self.has_default():
if callable(self.default):
defaults['initial'] = self.default
defaults['show_hidden_initial'] = True
else:
defaults['initial'] = self.get_default()
if self.choices:
# Fields with choices get special treatment.
include_blank = (self.blank or
not (self.has_default() or 'initial' in kwargs))
defaults['choices'] = self.get_choices(include_blank=include_blank)
defaults['coerce'] = self.to_python
if self.null:
defaults['empty_value'] = None
if choices_form_class is not None:
form_class = choices_form_class
else:
form_class = forms.TypedChoiceField
# Many of the subclass-specific formfield arguments (min_value,
# max_value) don't apply for choice fields, so be sure to only pass
# the values that TypedChoiceField will understand.
for k in list(kwargs):
if k not in ('coerce', 'empty_value', 'choices', 'required',
'widget', 'label', 'initial', 'help_text',
'error_messages', 'show_hidden_initial', 'disabled'):
del kwargs[k]
defaults.update(kwargs)
if form_class is None:
form_class = forms.CharField
return form_class(**defaults)
示例8: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def __init__(self, *args, **kwargs):
super(ProposalVoteForm, self).__init__(*args, **kwargs)
# Use a radio select widget instead of a dropdown for the score.
self.fields["score"] = forms.TypedChoiceField(
choices=ProposalVote.SCORES,
coerce=int,
empty_value=0,
widget=forms.RadioSelect(),
)
示例9: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def formfield(self, form_class=None, choices_form_class=None, **kwargs):
# Taken from django.db.models.fields.__init__
defaults = {'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text}
if self.has_default:
if callable(self.default):
defaults['initial'] = self.default
defaults['show_hidden_initial'] = True
else:
defaults['initial'] = self.get_default()
if self.choices:
# Fields with choices get special treatment.
include_blank = (self.blank or
not (self.has_default or 'initial' in kwargs))
defaults['choices'] = self.get_choices(include_blank=include_blank)
defaults['coerce'] = self.to_python
if self.null:
defaults['empty_value'] = None
if choices_form_class is not None:
form_class = choices_form_class
else:
form_class = forms.TypedChoiceField
# Many of the subclass-specific formfield arguments (min_value,
# max_value) don't apply for choice fields, so be sure to only pass
# the values that TypedChoiceField will understand.
for k in list(kwargs):
if k not in ('coerce', 'empty_value', 'choices', 'required',
'widget', 'label', 'initial', 'help_text',
'error_messages', 'show_hidden_initial'):
del kwargs[k]
defaults.update(kwargs)
if form_class is None:
form_class = forms.CharField
return form_class(**defaults)
示例10: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def formfield(self, form_class=None, choices_form_class=None, **kwargs):
"""
Returns a django.forms.Field instance for this database Field.
"""
defaults = {'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text}
if self.has_default():
if callable(self.default):
defaults['initial'] = self.default
defaults['show_hidden_initial'] = True
else:
defaults['initial'] = self.get_default()
if self.choices:
# Fields with choices get special treatment.
include_blank = (self.blank or
not (self.has_default() or 'initial' in kwargs))
defaults['choices'] = self.get_choices(include_blank=include_blank)
defaults['coerce'] = self.to_python
if self.null:
defaults['empty_value'] = None
if choices_form_class is not None:
form_class = choices_form_class
else:
form_class = forms.TypedChoiceField
# Many of the subclass-specific formfield arguments (min_value,
# max_value) don't apply for choice fields, so be sure to only pass
# the values that TypedChoiceField will understand.
for k in list(kwargs):
if k not in ('coerce', 'empty_value', 'choices', 'required',
'widget', 'label', 'initial', 'help_text',
'error_messages', 'show_hidden_initial', 'disabled'):
del kwargs[k]
defaults.update(kwargs)
if form_class is None:
form_class = forms.CharField
return form_class(**defaults)
示例11: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def formfield(self, form_class=forms.CharField, **kwargs):
"""
Returns a django.forms.Field instance for this database Field.
"""
defaults = {'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text}
if self.has_default():
if callable(self.default):
defaults['initial'] = self.default
defaults['show_hidden_initial'] = True
else:
defaults['initial'] = self.get_default()
if self.choices:
# Fields with choices get special treatment.
include_blank = (self.blank or
not (self.has_default() or 'initial' in kwargs))
defaults['choices'] = self.get_choices(include_blank=include_blank)
defaults['coerce'] = self.to_python
if self.null:
defaults['empty_value'] = None
form_class = forms.TypedChoiceField
# Many of the subclass-specific formfield arguments (min_value,
# max_value) don't apply for choice fields, so be sure to only pass
# the values that TypedChoiceField will understand.
for k in list(kwargs):
if k not in ('coerce', 'empty_value', 'choices', 'required',
'widget', 'label', 'initial', 'help_text',
'error_messages', 'show_hidden_initial'):
del kwargs[k]
defaults.update(kwargs)
return form_class(**defaults)
示例12: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def __init__(self, *args, **kwargs):
"""
Initialize the form.
Substitutes CharField with TypedChoiceField for the provider_id field.
"""
super(EnterpriseCustomerIdentityProviderAdminForm, self).__init__(*args, **kwargs)
idp_choices = utils.get_idp_choices()
help_text = ''
if saml_provider_configuration:
provider_id = self.instance.provider_id
url = reverse('admin:{}_{}_add'.format(
saml_provider_configuration._meta.app_label,
saml_provider_configuration._meta.model_name))
if provider_id:
identity_provider = utils.get_identity_provider(provider_id)
if identity_provider:
update_url = url + '?source={}'.format(identity_provider.pk)
help_text = '<p><a href="{update_url}" target="_blank">View "{identity_provider}" details</a><p>'.\
format(update_url=update_url, identity_provider=identity_provider.name)
else:
help_text += '<p style="margin-top:-5px;"> Make sure you have added a valid provider_id.</p>'
else:
help_text += '<p style="margin-top:-5px;"><a target="_blank" href={add_url}>' \
'Create a new identity provider</a></p>'.format(add_url=url)
if idp_choices is not None:
self.fields['provider_id'] = forms.TypedChoiceField(
choices=idp_choices,
label=_('Identity Provider'),
help_text=mark_safe(help_text),
)
示例13: test_application_optional_fields_match_field_type
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def test_application_optional_fields_match_field_type(self):
"""
The optional partner-specific data fields on Application should
correspond to the FIELD_TYPES used on the form. Additionally, each
should allow blank=True (since not all instances require all data),
except for BooleanFields, which should default False (i.e. they should
default to not requiring the data).
"""
for field in PARTNER_FORM_OPTIONAL_FIELDS:
# Ensure Application fields allow for empty data.
if not isinstance(Application._meta.get_field(field), models.BooleanField):
self.assertTrue(Application._meta.get_field(field).blank)
else:
self.assertFalse(Application._meta.get_field(field).default)
# Make sure the form fields we're using match what the model fields
# can record.
modelfield = Application._meta.get_field(field)
formfield = modelfield.formfield()
# While we simply use the ChoiceField for requested_access_duration field in the form, the model makes use
# of the TypedChoiceField, triggering a mismatch. We'll get around this by separately testing the fields.
if field == "requested_access_duration":
self.assertEqual(type(formfield), forms.TypedChoiceField)
self.assertEqual(type(FIELD_TYPES[field]), forms.ChoiceField)
break
self.assertEqual(type(formfield), type(FIELD_TYPES[field]))
示例14: test_typedchoicefield_1
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def test_typedchoicefield_1(self):
f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
self.assertEqual(1, f.clean('1'))
msg = "'Select a valid choice. 2 is not one of the available choices.'"
with self.assertRaisesMessage(ValidationError, msg):
f.clean('2')
示例15: test_typedchoicefield_2
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import TypedChoiceField [as 别名]
def test_typedchoicefield_2(self):
# Different coercion, same validation.
f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=float)
self.assertEqual(1.0, f.clean('1'))