本文整理汇总了Python中modeltranslation.utils.get_translation_fields函数的典型用法代码示例。如果您正苦于以下问题:Python get_translation_fields函数的具体用法?Python get_translation_fields怎么用?Python get_translation_fields使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_translation_fields函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, *args, **kwargs):
super(TranslationAdmin, self).__init__(*args, **kwargs)
trans_opts = translator.get_options_for_model(self.model)
# Replace original field with translation field for each language
if self.fields:
fields_new = list(self.fields)
for field in self.fields:
if field in trans_opts.fields:
index = fields_new.index(field)
translation_fields = get_translation_fields(field)
fields_new[index : index + 1] = translation_fields
self.fields = fields_new
if self.fieldsets:
fieldsets_new = list(self.fieldsets)
for (name, dct) in self.fieldsets:
if "fields" in dct:
tfields_new = []
for field in list(dct["fields"]):
if isinstance(field, tuple):
tfields = []
for f in field:
if f in trans_opts.fields:
tfields.extend(get_translation_fields(f))
else:
tfields.extend((f,))
# FIXME: Flatten nested tuples as they will break in the
# current tabs implementation. Normally we want:
# tfields_new.append(tuple(tfields))
tfields_new.append(tuple(tfields))
else:
if field in trans_opts.fields:
tfields_new.extend(get_translation_fields(field))
else:
tfields_new.extend((field,))
dct["fields"] = tuple(tfields_new)
self.fieldsets = fieldsets_new
if self.list_editable:
editable_new = list(self.list_editable)
display_new = list(self.list_display)
for field in self.list_editable:
if field in trans_opts.fields:
index = editable_new.index(field)
display_index = display_new.index(field)
translation_fields = get_translation_fields(field)
editable_new[index : index + 1] = translation_fields
display_new[display_index : display_index + 1] = translation_fields
self.list_editable = editable_new
self.list_display = display_new
if self.prepopulated_fields:
prepopulated_fields_new = dict(self.prepopulated_fields)
for (k, v) in self.prepopulated_fields.items():
if v[0] in trans_opts.fields:
translation_fields = get_translation_fields(v[0])
prepopulated_fields_new[k] = tuple([translation_fields[0]])
self.prepopulated_fields = prepopulated_fields_new
示例2: __init__
def __init__(self, *args, **kwargs):
super(TranslationAdmin, self).__init__(*args, **kwargs)
trans_opts = translator.get_options_for_model(self.model)
# Replace original field with translation field for each language
if self.fields:
fields_new = list(self.fields)
for field in self.fields:
if field in trans_opts.fields:
index = fields_new.index(field)
translation_fields = get_translation_fields(field)
fields_new[index:index + 1] = translation_fields
self.fields = fields_new
if self.fieldsets:
fieldsets_new = list(self.fieldsets)
for (name, dct) in self.fieldsets:
if 'fields' in dct:
# TODO: Add support for grouped fieldsets
# (see issue 52 for details)
fields_new = list(dct['fields'])
for field in dct['fields']:
if field in trans_opts.fields:
index = fields_new.index(field)
translation_fields = get_translation_fields(field)
fields_new[index:index + 1] = translation_fields
dct['fields'] = fields_new
self.fieldsets = fieldsets_new
if self.list_editable:
editable_new = list(self.list_editable)
display_new = list(self.list_display)
for field in self.list_editable:
if field in trans_opts.fields:
index = editable_new.index(field)
display_index = display_new.index(field)
translation_fields = get_translation_fields(field)
editable_new[index:index + 1] = translation_fields
display_new[display_index:display_index + 1] =\
translation_fields
self.list_editable = editable_new
self.list_display = display_new
if self.prepopulated_fields:
prepopulated_fields_new = dict(self.prepopulated_fields)
for (k, v) in self.prepopulated_fields.items():
if v[0] in trans_opts.fields:
translation_fields = get_translation_fields(v[0])
prepopulated_fields_new[k] = tuple([translation_fields[0]])
self.prepopulated_fields = prepopulated_fields_new
示例3: replace_orig_field
def replace_orig_field(self, option):
"""
Replaces each original field in `option` that is registered for
translation by its translation fields.
Returns a new list with replaced fields. If `option` contains no
registered fields, it is returned unmodified.
>>> print self.trans_opts.fields
('title',)
>>> get_translation_fields(self.trans_opts.fields[0])
['title_de', 'title_en']
>>> self.replace_orig_field(['title', 'url'])
['title_de', 'title_en', 'url']
"""
# TODO: Handle nested lists to display multiple fields on same line.
if option:
option_new = list(option)
for opt in option:
if opt in self.trans_opts.fields:
index = option_new.index(opt)
translation_fields = get_translation_fields(opt)
option_new[index:index + 1] = translation_fields
option = option_new
return option
示例4: replace_orig_field
def replace_orig_field(self, option):
"""
Replaces each original field in `option` that is registered for
translation by its translation fields.
Returns a new list with replaced fields. If `option` contains no
registered fields, it is returned unmodified.
>>> print(self.trans_opts.fields.keys())
['title',]
>>> get_translation_fields(self.trans_opts.fields.keys()[0])
['title_de', 'title_en']
>>> self.replace_orig_field(['title', 'url'])
['title_de', 'title_en', 'url']
Note that grouped fields are flattened. We do this because:
1. They are hard to handle in the jquery-ui tabs implementation
2. They don't scale well with more than a few languages
3. It's better than not handling them at all (okay that's weak)
>>> self.replace_orig_field((('title', 'url'), 'email', 'text'))
['title_de', 'title_en', 'url_de', 'url_en', 'email_de', 'email_en', 'text']
"""
if option:
option_new = list(option)
for opt in option:
if opt in self.trans_opts.fields:
index = option_new.index(opt)
option_new[index : index + 1] = get_translation_fields(opt)
elif isinstance(opt, (tuple, list)) and ([o for o in opt if o in self.trans_opts.fields]):
index = option_new.index(opt)
option_new[index : index + 1] = self.replace_orig_field(opt)
option = option_new
return option
示例5: _patch_prepopulated_fields
def _patch_prepopulated_fields(self):
if self.prepopulated_fields:
prepopulated_fields_new = dict(self.prepopulated_fields)
for (k, v) in self.prepopulated_fields.items():
if v[0] in self.trans_opts.fields:
translation_fields = get_translation_fields(v[0])
prepopulated_fields_new[k] = tuple([translation_fields[0]])
self.prepopulated_fields = prepopulated_fields_new
示例6: get_readonly_fields
def get_readonly_fields(self, request, obj=None):
if request.user.is_superuser:
return self.readonly_fields
fields = list(self.readonly_fields)
fields.extend(['user', 'nickname', 'birth_place', 'birth_date', 'email'])
fields.extend(get_translation_fields('subtitle'))
return tuple(fields)
示例7: _patch_list_editable
def _patch_list_editable(self):
if self.list_editable:
editable_new = list(self.list_editable)
display_new = list(self.list_display)
for field in self.list_editable:
if field in self.trans_opts.fields:
index = editable_new.index(field)
display_index = display_new.index(field)
translation_fields = get_translation_fields(field)
editable_new[index:index + 1] = translation_fields
display_new[display_index:display_index + 1] = translation_fields
self.list_editable = editable_new
self.list_display = display_new
示例8: validate_unique
def validate_unique(self):
"""
Calls the instance's validate_unique() method and updates the form's
validation errors if any were raised.
"""
if translator:
for fname in get_translation_fields('name'):
if getattr(self.instance, fname, None) == "":
setattr(self.instance, fname, None)
try:
self.instance.validate_unique()
except ValidationError as e:
self._update_errors(e)
示例9: __init__
def __init__(self, *args, **kwargs):
super(TranslationAdmin, self).__init__(*args, **kwargs)
trans_opts = translator.get_options_for_model(self.model)
# Replace original field with translation field for each language
if self.fields:
fields_new = list(self.fields)
for field in self.fields:
if field in trans_opts.fields:
index = fields_new.index(field)
translation_fields = get_translation_fields(field)
fields_new[index:index + 1] = translation_fields
self.fields = fields_new
# Simple policy: if the admin class already defines a fieldset, we
# leave it alone and assume the author has done whatever grouping for
# translated fields they desire:
if self.fieldsets:
fieldsets_new = list(self.fieldsets)
for (name, dct) in self.fieldsets:
if 'fields' in dct:
fields_new = list(dct['fields'])
for field in dct['fields']:
if field in trans_opts.fields:
index = fields_new.index(field)
translation_fields = get_translation_fields(field)
fields_new[index:index + 1] = translation_fields
dct['fields'] = fields_new
self.fieldsets = fieldsets_new
else:
# If there aren't any existing fieldsets, we'll automatically
# create one to group each translated field's localized fields:
non_translated_fields = [
f.name for f in self.opts.fields if (
# The original translation field:
f.name not in trans_opts.fields
# The auto-generated fields for translations:
and f.name not in trans_opts.localized_fieldnames_rev
# Avoid including the primary key field:
and f is not self.opts.auto_field
# Avoid non-editable fields
and f.editable
)
]
self.fieldsets = [
('', {'fields': non_translated_fields}),
]
for orig_field, trans_fields in trans_opts.localized_fieldnames.items():
# Extract the original field's verbose_name for use as this
# fieldset's label - using ugettext_lazy in your model
# declaration can make that translatable:
label = self.model._meta.get_field(orig_field).verbose_name
self.fieldsets.append((label, {
"fields": trans_fields,
"classes": ("modeltranslations",)
}))
if self.list_editable:
editable_new = list(self.list_editable)
display_new = list(self.list_display)
for field in self.list_editable:
if field in trans_opts.fields:
index = editable_new.index(field)
display_index = display_new.index(field)
translation_fields = get_translation_fields(field)
editable_new[index:index + 1] = translation_fields
display_new[display_index:display_index + 1] =\
translation_fields
self.list_editable = editable_new
self.list_display = display_new
if self.prepopulated_fields:
prepopulated_fields_new = dict(self.prepopulated_fields)
for (k, v) in self.prepopulated_fields.items():
if v[0] in trans_opts.fields:
translation_fields = get_translation_fields(v[0])
prepopulated_fields_new[k] = tuple([translation_fields[0]])
self.prepopulated_fields = prepopulated_fields_new