• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python utils.get_translation_fields函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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
开发者ID:beholderrk,项目名称:django-modeltranslation,代码行数:59,代码来源:admin.py


示例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
开发者ID:fgallina,项目名称:django-modeltranslation,代码行数:50,代码来源:admin.py


示例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
开发者ID:COEXCZ,项目名称:django-modeltranslation,代码行数:25,代码来源:admin.py


示例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
开发者ID:20tab,项目名称:django-modeltranslation,代码行数:35,代码来源:admin.py


示例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
开发者ID:COEXCZ,项目名称:django-modeltranslation,代码行数:8,代码来源:admin.py


示例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)
开发者ID:FUUK,项目名称:fuuk,代码行数:8,代码来源:options.py


示例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
开发者ID:APSL,项目名称:django-modeltranslation,代码行数:13,代码来源:admin.py


示例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)
开发者ID:emacsway,项目名称:django-geo,代码行数:13,代码来源:forms.py


示例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
开发者ID:GunioRobot,项目名称:django-modeltranslation,代码行数:81,代码来源:admin.py



注:本文中的modeltranslation.utils.get_translation_fields函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python diff.get_changes_between_models函数代码示例发布时间:2022-05-27
下一篇:
Python utils.get_language函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap