1

我有一个包含动态数量的图像和描述的主要对象。

因此,我正在使用外键/表格内联组合!

class InstructionsModel(TranslatableModel):
  instruction = models.ForeignKey(InstructionModel, related_name='instructions_plural_project')
  mainImage = FilerImageField(verbose_name=('Marker image'), 
                            blank=True, null=True, 
                            on_delete=models.SET_NULL)
  translations = TranslatedFields(
    title=models.CharField(_('Instruction title'), max_length=255),
    introduction=RichTextField(_('Instruction introduction or subtitle'), default='', blank=True),
    description=RichTextField(_('Instruction description'), default='', blank=True),
  )

  _metadata = {
    'title': 'get_title',
  }

  def get_title(self):
    title = self.safe_translation_getter('meta_title', any_language=True)
    if not title:
        title = self.safe_translation_getter('title', any_language=True)
    return title

  def __str__(self):
    title = self.safe_translation_getter('title', any_language=True)
    return title if title is not None else '(not translated)'

  def save(self, *args, **kwargs):
    super(InstructionsModel, self).save(*args, **kwargs)
    main_lang = self.get_current_language()
    for lang in self.get_available_languages():
        self.set_current_language(lang)
    self.set_current_language(main_lang)
    self.save_translations()

但问题是这些描述中的每一个都必须是可翻译的。

因此管理员看起来像这样:

class InstructionsAdminInline(admin.TabularInline, TranslatableAdmin):
    model = InstructionsModel
    extra = 3
    form = InstructionsMultiAdminForm

    list_display = [
        '__str__',
    ]

    fieldsets = [
        ('test', {
            'fields': [('title', 'introduction'), 'description', 'mainImage']
        }),
    ]

用一个非常简单的形式(向你展示我的想法已经用完了):

class InstructionsMultiAdminForm(TranslatableModelForm):
    class Meta(TranslatableModelForm):
        model = InstructionsModel
        exclude = ()

    def __init__(self, *args, **kwargs):
        super(InstructionsMultiAdminForm, self).__init__(*args, **kwargs)

但是这个错误仍然存​​在:

 File "/Users/JayCee/education-proj/homeroom/homeroom/django_instructions/admin.py", line 49, in <module>
    admin.site.register(InstructionModel, InstructionAdmin)
  File "/Users/JayCee/education-proj/homeroom/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 109, in register
    system_check_errors.extend(admin_obj.check())
  File "/Users/JayCee/education-proj/homeroom/lib/python3.5/site-packages/django/contrib/admin/options.py", line 113, in check
    return self.checks_class().check(self, **kwargs)
  File "/Users/JayCee/education-proj/homeroom/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 498, in check
    errors.extend(self._check_inlines(admin_obj))
  File "/Users/JayCee/education-proj/homeroom/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 536, in _check_inlines
    for index, item in enumerate(obj.inlines)
  File "/Users/JayCee/education-proj/homeroom/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 536, in <listcomp>
    for index, item in enumerate(obj.inlines)
  File "/Users/JayCee/education-proj/homeroom/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 567, in _check_inlines_item
    return inline(model, obj.admin_site).check()
  File "/Users/JayCee/education-proj/homeroom/lib/python3.5/site-packages/django/contrib/admin/options.py", line 1765, in __init__
    super(InlineModelAdmin, self).__init__()
TypeError: __init__() missing 2 required positional arguments: 'model' and 'admin_site'

(堆栈跟踪开头的第 49 行指的是admin.site.register(InstructionModel, InstructionAdmin)

如果我从 InstructionAdmin 中删除此行:

class InstructionAdmin(TranslatableAdmin):
    inlines = [ InstructionsAdminInline ]

错误停止。

有人知道吗?

非常感谢!

4

1 回答 1

1

问题来自翻译。django-parler 为此提供了一个特定的类:

TranslatableTabularInline

一旦它完成,不再有错误。

from parler.admin import TranslatableAdmin, TranslatableTabularInline

class InstructionsAdminInline(TranslatableTabularInline):
    model = InstructionsModel
于 2017-01-31T10:33:54.143 回答