如何修复TabularInline管理员Django中的额外字段错误?

时间:2019-05-13 11:46:57

标签: django django-admin

我在Django admin中的TabularInline表单以错误的方式显示字段。仅标题以灰色重复,并且get_url链接的位置不正确。

代码真的很简单,我不能自己调试它。我尝试设置字段并排除不显示title_cs和get_url字段而没有成功的字段,这些字段是在原始字段上呈现的。

class InfographicInLine(admin.TabularInline):
    model = Infographic
    extra = 0
    # list_display = (
    #     'title_cs', 'get_url', 'show_on_frontpage', 'author',
    #     'image_tag')

    list_editable = (None,)

    exclude = ('title_cs', 'get_url')
    fields = (
         'title_en', 'author', 'keywords_cs',)


class InfographicAdmin(ActiveFlagModelAdmin):
    list_display = (
        'title_cs', 'title_en', 'get_url', 'section', 'show_on_frontpage', 'author', 'created_date',
        'last_modified',
        'image_tag')
    list_editable = ('title_en', 'show_on_frontpage')
    list_filter = ('section', 'created_date', 'last_modified', 'author')
    fields = (
        'title_cs', 'title_en', 'section', 'infographic_url_cs', 'infographic_url_en', ('thumbnail_cs', 'image_tag'),
        'thumbnail_en', 'author', 'keywords_cs',
        'keywords_en')
    search_fields = ('title_cs', 'title_en', 'description_cs', 'description_en',)

    exclude = ('slug', 'embed_code_cs', 'embed_code_en')
    # prepopulated_fields = {'slug': ('title_cs',)}

    readonly_fields = ('image_tag',)

    def image_tag(self, obj):
        if obj.thumbnail_cs:
            return mark_safe('<img height="75" width="105" src="/media/%s" />' % obj.thumbnail_cs)
        else:
            return ""

    def get_url(self, obj):
        if obj:
            return mark_safe('<a href="' + settings.STARFOS_BASE_URL + obj.get_absolute_url() +
                             '" >' + 'Ukázat na stránce' + '</a>')
        else:
            return None


register(Infographic, InfographicAdmin)


@admin.register(Section)
class SectionAdmin(ActiveFlagModelAdmin):
    list_display = ('title_cs', 'title_en', 'description_en', 'description_cs',)
    list_editable = ('title_cs', 'title_en',)
    list_filter = ('title_cs', 'title_en')
    exclude = ('slug',)
    inlines = [InfographicInLine, ]

我希望仅在管理员的TabularInline部分中呈现提供的字段。现在,我在所需的字段上又渲染了两个额外的字段。查看图片中表格的第一列。

First column is rendered wrong

0 个答案:

没有答案