渲染StackedInline字段时如何覆盖str方法? [英] How to override the str method when rendering StackedInline field?

查看:67
本文介绍了渲染StackedInline字段时如何覆盖str方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多关系:

class GroupeCategoriesCategorie(models.Model):
    groupe_categories = models.ForeignKey(GroupeCategories,
                                          related_name='groupe',
                                          verbose_name=_(u'Groupe'))
    categorie = models.ForeignKey(Categorie,
                                  related_name='categorie',
                                  verbose_name=_(u'Catégorie'))

    def __str__(self):
        return _(u'{} / {}').format(self.groupe_categories, self.categorie)

我总是需要像 __ str __ 这样的表示形式在管理员中呈现时,除外。为什么?我的 admin.py 看起来像这样:

I always need a representation like the __str__ method before except when rendering in the admin. Why? My admin.py looks like that:

class GroupeCategoriesCategoriesInline(CollapsedStackedInline):
    model = GroupeCategories.liens.through
    fk_name = 'groupe_categories'
    raw_id_fields = ('categorie',)
    extra = 0    

class GroupeCategoriesAdmin(admin.ModelAdmin):

    fields = ('description', 'exemple',)
    inlines = (GroupeCategoriesCategoriesInline,)

结果如下:

如您所见,当您编辑 GroupeCategories 时,首先会看到 Description 字段,以及此 Description 会在紧接着的每一行重复出现,这很丑。我想覆盖 __ str __ 方法,但仅在此处

As you can see, when you edit a GroupeCategories, you see the Description field first, and this description is repeated for each manytomany row just after... this is ugly. I'd like to override the __str__ method, but only here. How do you do that?

推荐答案

您可以轻松覆盖内联使用的模板:

You can easily override the template used by the inline:

class GroupeCategoriesCategoriesInline(admin.StackedInline):
    [...]
    template = 'yourapp/admin/stackedinline.html'

获取此模板的副本:

< a href = https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/edit_inline/stacked.html rel = nofollow> https://github.com/ django / django / blob / master / django / contrib / admin / templates / admin / edit_inline / stacked.html

这篇关于渲染StackedInline字段时如何覆盖str方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆