本文整理汇总了Python中tri.form.Field类的典型用法代码示例。如果您正苦于以下问题:Python Field类的具体用法?Python Field怎么用?Python Field使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Field类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_field_from_model_supports_all_types
def test_field_from_model_supports_all_types():
from django.db.models import fields
not_supported = []
blacklist = {
'AutoField',
'Field',
'BigAutoField',
'BinaryField',
'FilePathField',
'GenericIPAddressField',
'IPAddressField',
'NullBooleanField',
'SlugField',
'DurationField',
'UUIDField'
}
field_type_names = [x for x in dir(fields) if x.endswith('Field') and x not in blacklist]
for name in field_type_names:
field_type = getattr(fields, name)
try:
Field.from_model(model=Foo, model_field=field_type())
except AssertionError: # pragma: no cover
not_supported.append(name)
assert not_supported == []
示例2: test_field_from_model_blank_handling
def test_field_from_model_blank_handling():
from django.db.models import CharField
subject = Field.from_model(model=Foo, model_field=CharField(blank=False))
assert True is subject.parse_empty_string_as_none
subject = Field.from_model(model=Foo, model_field=CharField(blank=True))
assert False is subject.parse_empty_string_as_none
示例3: test_field_from_model_many_to_many
def test_field_from_model_many_to_many():
Foo.objects.create(foo=2)
Foo.objects.create(foo=3)
Foo.objects.create(foo=5)
choices = Field.from_model(FieldFromModelManyToManyTest, 'foo_many_to_many').choices
assert isinstance(choices, QuerySet)
assert set(choices) == set(Foo.objects.all())
示例4: test_comma_separated_errors_on_validation
def test_comma_separated_errors_on_validation():
assert Form(
data=dict(foo='5, 7'),
fields=[Field.comma_separated(Field(name='foo', is_valid=lambda parsed_data, **_: (False, 'foo %s!' % parsed_data)))]).fields[0].errors == {
u'Invalid value "5": foo 5!',
u'Invalid value "7": foo 7!',
}
示例5: test_field_from_model_foreign_key
def test_field_from_model_foreign_key():
Foo.objects.create(foo=2)
Foo.objects.create(foo=3)
Foo.objects.create(foo=5)
choices = Field.from_model(FieldFromModelForeignKeyTest, 'foo_fk').choices
assert isinstance(choices, QuerySet)
assert set(choices) == set(Foo.objects.all())
示例6: test_render_datetime_iso
def test_render_datetime_iso():
table = Form(fields=[
Field.datetime(
name='foo',
initial=datetime(2001, 2, 3, 12, 13, 14, 7777))
]).table()
assert '2001-02-03 12:13:14' in table
assert '7777' not in table
示例7: test_radio
def test_radio():
choices = [
'a',
'b',
'c',
]
soup = BeautifulSoup(Form(data=Data(foo='a'), fields=[Field.radio(name='foo', choices=choices)]).validate().table())
assert len(soup.find_all('input')) == len(choices) + 1 # +1 for AVOID_EMPTY_FORM
assert [x.attrs['value'] for x in soup.find_all('input') if 'checked' in x.attrs] == ['a']
示例8: test_comma_separated_errors_on_parse
def test_comma_separated_errors_on_parse():
def raise_always_value_error(string_value, **_):
raise ValueError('foo %s!' % string_value)
def raise_always_validation_error(string_value, **_):
raise ValidationError(['foo %s!' % string_value, 'bar %s!' % string_value])
assert Form(
data=Data(foo='5, 7'),
fields=[Field.comma_separated(Field(name='foo', parse=raise_always_value_error))]).validate().fields[0].errors == {
u'Invalid value "5": foo 5!',
u'Invalid value "7": foo 7!'}
assert Form(
data=Data(foo='5, 7'),
fields=[Field.comma_separated(Field(name='foo', parse=raise_always_validation_error))]).validate().fields[0].errors == {
u'Invalid value "5": foo 5!',
u'Invalid value "5": bar 5!',
u'Invalid value "7": foo 7!',
u'Invalid value "7": bar 7!'}
示例9: test_boolean_initial_true
def test_boolean_initial_true():
fields = [Field.boolean(name='foo', initial=True), Field(name='bar', required=False)]
form = Form(data=Data(), fields=fields).validate()
assert form.fields_by_name['foo'].value is True
# If there are arguments, but not for key foo it means checkbox for foo has been unchecked.
# Field foo should therefore be false.
form = Form(data=Data(bar='baz'), fields=fields).validate()
assert form.fields_by_name['foo'].value is False
form = Form(data=Data(foo='on', bar='baz'), fields=fields).validate()
assert form.fields_by_name['foo'].value is True
示例10: test_choices_from_query_set
def test_choices_from_query_set():
user = User.objects.create(username='foo')
assert [x.pk for x in Form(fields=[Field.multi_choice_queryset(name='foo', model=User, choices=User.objects.all())]).validate().fields[0].choices] == [user.pk]
示例11: test_help_text_from_model
def test_help_text_from_model():
assert Form(data=Data(foo='1'), fields=[Field.from_model(model=Foo, field_name='foo')], model=Foo).validate().fields[0].help_text == 'foo_help_text'
示例12: test_multi_choice
def test_multi_choice():
soup = BeautifulSoup(Form(data=Data(foo=['0']), fields=[Field.multi_choice(name='foo', choices=['a'])]).validate().table())
assert [x.attrs['multiple'] for x in soup.find_all('select')] == ['']
示例13: test_password
def test_password():
assert ' type="password" ' in Form(data=Data(foo='1'), fields=[Field.password(name='foo')]).validate().table()
示例14: test_hidden
def test_hidden():
soup = BeautifulSoup(Form(data=Data(foo='1'), fields=[Field.hidden(name='foo')]).validate().table())
assert [(x.attrs['type'], x.attrs['value']) for x in soup.find_all('input')] == [('hidden', '1'), ('hidden', '-')]
示例15: test_json_parsing
def test_json_parsing():
# NOTE: Parsing json input requires that any numbers that come in must avoid the string strip
f = Form(data={'foo': 1}, fields=[Field.integer(name='foo', strip_input=False)])
assert f.is_valid()
assert f.fields_by_name['foo'].value == 1