本文整理汇总了Python中tri.declarative.setdefaults_path函数的典型用法代码示例。如果您正苦于以下问题:Python setdefaults_path函数的具体用法?Python setdefaults_path怎么用?Python setdefaults_path使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setdefaults_path函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: datetime
def datetime(**kwargs):
iso_formats = [
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%d %H:%M',
'%Y-%m-%d %H',
]
def datetime_parse(string_value, **_):
errors = []
for iso_format in iso_formats:
try:
return datetime.strptime(string_value, iso_format)
except ValueError as e:
errors.append('%s' % e)
assert errors
raise ValidationError('Time data "%s" does not match any of the formats %s' % (string_value, ', '.join('"%s"' % x for x in iso_formats)))
def datetime_render_value(value, **_):
return value.strftime(iso_formats[0]) if value else ''
setdefaults_path(
kwargs,
parse=datetime_parse,
render_value=datetime_render_value,
)
return Field(**kwargs)
示例2: member_from_model
def member_from_model(model, factory_lookup, defaults_factory, factory_lookup_register_function=None, field_name=None, model_field=None, **kwargs):
if model_field is None:
# noinspection PyProtectedMember
model_field = model._meta.get_field(field_name)
setdefaults_path(
kwargs,
defaults_factory(model_field),
name=field_name,
)
factory = factory_lookup.get(type(model_field), MISSING)
if factory is MISSING:
for django_field_type, func in reversed(factory_lookup.items()):
if isinstance(model_field, django_field_type):
factory = func
break
if factory is MISSING: # pragma: no cover
message = 'No factory for %s.' % type(model_field)
if factory_lookup_register_function is not None:
message += ' Register a factory with %s, you can also register one that returns None to not handle this field type' % factory_lookup_register_function.__name__
raise AssertionError(message)
return factory(model_field=model_field, model=model, **kwargs) if factory else None
示例3: email
def email(**kwargs):
setdefaults_path(
kwargs,
input_type='email',
parse=lambda string_value, **_: validate_email(string_value) or string_value,
)
return Field(**kwargs)
示例4: many_to_many_factory
def many_to_many_factory(model_field, **kwargs):
setdefaults_path(
kwargs,
choices=model_field.rel.to._default_manager.all(),
model=model_field.foreign_related_fields[0].model,
)
return Column.multi_choice_queryset(model_field=model_field, **kwargs)
示例5: foreign_key_factory
def foreign_key_factory(model_field, **kwargs):
setdefaults_path(
kwargs,
choices=model_field.foreign_related_fields[0].model.objects.all(),
model=model_field.foreign_related_fields[0].model,
)
return Column.choice_queryset(model_field=model_field, **kwargs)
示例6: foreign_key_factory
def foreign_key_factory(model_field, **kwargs):
setdefaults_path(
kwargs,
choices=model_field.foreign_related_fields[0].model.objects.all(),
)
kwargs['model'] = model_field.foreign_related_fields[0].model
return Field.choice_queryset(**kwargs)
示例7: test_setdefatults_path_retain_empty
def test_setdefatults_path_retain_empty():
actual = setdefaults_path(Namespace(a=Namespace()), a__b=Namespace())
expected = Namespace(a__b=Namespace())
assert expected == actual
actual = setdefaults_path(Namespace(), attrs__class=Namespace())
expected = Namespace(attrs__class=Namespace())
assert expected == actual
示例8: multi_choice_queryset
def multi_choice_queryset(**kwargs):
setdefaults_path(
kwargs,
attrs__multiple=True,
choice_to_option=lambda form, field, choice: (choice, choice.pk, "%s" % choice, field.value_list and choice in field.value_list),
is_list=True,
)
return Field.choice_queryset(**kwargs)
示例9: integer
def integer(**kwargs):
def int_parse(string_value, **_):
return int(string_value)
setdefaults_path(
kwargs,
parse=int_parse,
)
return Field(**kwargs)
示例10: decimal
def decimal(**kwargs):
def decimal_parse(string_value, **_):
return Decimal(string_value)
setdefaults_path(
kwargs,
parse=decimal_parse,
)
return Field(**kwargs)
示例11: many_to_many_factory
def many_to_many_factory(model_field, **kwargs):
setdefaults_path(
kwargs,
choices=model_field.rel.to.objects.all(),
read_from_instance=lambda field, instance: getattr_path(instance, field.attr).all(),
extra__django_related_field=True,
)
kwargs['model'] = model_field.rel.to
return Field.multi_choice_queryset(**kwargs)
示例12: phone_number
def phone_number(**kwargs):
def phone_number_is_valid(parsed_data, **_):
return re.match(r'^\+\d{1,3}(( |-)?\(\d+\))?(( |-)?\d+)+$', parsed_data, re.IGNORECASE), 'Please use format +<country code> (XX) XX XX. Example of US number: +1 (212) 123 4567 or +1 212 123 4567'
setdefaults_path(
kwargs,
is_valid=phone_number_is_valid,
)
return Field(**kwargs)
示例13: email
def email(**kwargs):
def email_parse(string_value, **_):
return validate_email(string_value) or string_value
setdefaults_path(
kwargs,
input_type='email',
parse=email_parse,
)
return Field(**kwargs)
示例14: url
def url(**kwargs):
def url_parse(string_value, **_):
return URLValidator(string_value) or string_value
setdefaults_path(
kwargs,
input_type='email',
parse=url_parse,
)
return Field(**kwargs)
示例15: info
def info(value, **kwargs):
"""
Shortcut to create an info entry.
"""
setdefaults_path(
kwargs,
initial=value,
editable=False,
attr=None,
)
return Field(**kwargs)