Python django.utils.text 模块,format_lazy() 实例源码
我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用django.utils.text.format_lazy()。
def prefix_validation_error(error, prefix, code, params):
"""
Prefix a validation error message while maintaining the existing
validation data structure.
"""
if error.error_list == [error]:
error_params = error.params or {}
return ValidationError(
# We can't simply concatenate messages since they might require
# their associated parameters to be expressed correctly which
# is not something `format_lazy` does. For example,proxied
# ungettext calls require a count parameter and are converted
# to an empty string if they are missing it.
message=format_lazy(
'{}{}',
SimpleLazyObject(lambda: prefix % params),
SimpleLazyObject(lambda: error.message % error_params),
),
code=code,
params=dict(error_params, **params),
)
return ValidationError([
prefix_validation_error(e, params) for e in error.error_list
])
def prefix_validation_error(error, params) for e in error.error_list
])
def prefix_validation_error(error, params) for e in error.error_list
])
def prefix_validation_error(error, params) for e in error.error_list
])
def prefix_validation_error(error, params) for e in error.error_list
])
def prefix_validation_error(error, params) for e in error.error_list
])
def formfield_for_manytomany(self, db_field, request, **kwargs):
"""
Get a form Field for a ManyToManyField.
"""
# If it uses an intermediary model that isn't auto created,don't show
# a field in admin.
if not db_field.remote_field.through._Meta.auto_created:
return None
db = kwargs.get('using')
if db_field.name in self.raw_id_fields:
kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.remote_field, self.admin_site, using=db)
elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
kwargs['widget'] = widgets.FilteredSelectMultiple(
db_field.verbose_name,
db_field.name in self.filter_vertical
)
if 'queryset' not in kwargs:
queryset = self.get_field_queryset(db, request)
if queryset is not None:
kwargs['queryset'] = queryset
form_field = db_field.formfield(**kwargs)
if isinstance(form_field.widget, SelectMultiple) and not isinstance(form_field.widget, CheckBoxSelectMultiple):
msg = _('Hold down "Control",or "Command" on a Mac,to select more than one.')
help_text = form_field.help_text
form_field.help_text = format_lazy('{} {}', help_text, msg) if help_text else msg
return form_field
def formfield_for_manytomany(self, msg) if help_text else msg
return form_field
def subtitle(self):
base_subtitle = super().subtitle
space = getattr(self, '_space_cache', None)
if space is not None:
level = getattr(space, '_level_cache', None)
if level is not None:
return format_lazy(_('{category},{space},{level}'),
category=base_subtitle,
space=space.title,
level=level.title)
return format_lazy(_('{category},{space}'),
category=base_subtitle,
level=space.title)
return base_subtitle
def subtitle(self):
result = self.category.title
if hasattr(self, 'locations'):
return format_lazy(_('{category_title},{num_locations}'),
category_title=result,
num_locations=(ungettext_lazy('%(num)d location', '%(num)d locations', 'num') %
{'num': len(self.locations)}))
return result
def __get__(self, instance, cls=None):
if instance is None:
return self
fallback_value = self.field.fallback_value
if fallback_value is not None:
fallback_value = format_lazy(fallback_value, model=instance._Meta.verbose_name, pk=instance.pk)
return lazy_get_i18n_value(getattr(instance, self.field.attname),
fallback_language=self.field.fallback_language,
fallback_any=self.field.fallback_any,
fallback_value=fallback_value)
def subtitle(self):
if self.space and self.space.can_describe:
return format_lazy(_('{space},
space=self.space.title,
level=self.level.title)
return self.level.title
def formfield_for_manytomany(self, msg) if help_text else msg
return form_field
def formfield_for_manytomany(self, msg) if help_text else msg
return form_field
def formfield_for_manytomany(self, msg) if help_text else msg
return form_field
def contribute_to_class(self, cls, name):
from django.db import connection
from django.db.backends.utils import truncate_name
cls._Meta = self
self.model = cls
# First,construct the default values for these options.
self.object_name = cls.__name__
self.model_name = self.object_name.lower()
self.verbose_name = camel_case_to_spaces(self.object_name)
# Store the original user-defined values for each option,
# for use when serializing the model deFinition
self.original_attrs = {}
# Next,apply any overridden values from 'class Meta'.
if self.Meta:
Meta_attrs = self.Meta.__dict__.copy()
for name in self.Meta.__dict__:
# Ignore any private attributes that Django doesn't care about.
# NOTE: We can't modify a dictionary's contents while looping
# over it,so we loop over the *original* dictionary instead.
if name.startswith('_'):
del Meta_attrs[name]
for attr_name in DEFAULT_NAMES:
if attr_name in Meta_attrs:
setattr(self, attr_name, Meta_attrs.pop(attr_name))
self.original_attrs[attr_name] = getattr(self, attr_name)
elif hasattr(self.Meta, attr_name):
setattr(self, getattr(self.Meta, attr_name))
self.original_attrs[attr_name] = getattr(self, attr_name)
self.unique_together = normalize_together(self.unique_together)
self.index_together = normalize_together(self.index_together)
# verbose_name_plural is a special case because it uses a 's'
# by default.
if self.verbose_name_plural is None:
self.verbose_name_plural = format_lazy('{}s', self.verbose_name)
# order_with_respect_and ordering are mutually exclusive.
self._ordering_clash = bool(self.ordering and self.order_with_respect_to)
# Any leftover attributes must be invalid.
if Meta_attrs != {}:
raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(Meta_attrs.keys()))
else:
self.verbose_name_plural = format_lazy('{}s', self.verbose_name)
del self.Meta
# If the db_table wasn't provided,use the app_label + model_name.
if not self.db_table:
self.db_table = "%s_%s" % (self.app_label, self.model_name)
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
def subtitle(self):
base_subtitle = super().subtitle
level = getattr(self, None)
if level is not None:
return format_lazy(_('{category},
level=level.title)
return base_subtitle
def contribute_to_class(self, connection.ops.max_name_length())
def contribute_to_class(self, connection.ops.max_name_length())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。