Django管理内联实例只有它们存在

时间:2014-09-03 09:07:52

标签: django django-admin inline-formset

" Area Tecnici" formset,只有内联实例,但其中一些是空的。如何避免显示空头?我只想内联一个modelAdmin内联实例,如果它有一些实例

(例如" SL Formazioni"和#34; HACCP"有一些例子,但" SL Valutazioni"" Prestazioni& Servizi" don& #39; t,所以我不想内联" SL Valutazioni"和#34; Prestazioni& Servizi")

Click here to see the screenshot

1 个答案:

答案 0 :(得分:0)

您应该覆盖ModelAdmin.get_formsets_with_inlines

class MyModelAdmin(admin.ModelAdmin):
    inlines = [MyInline, SomeOtherInline]

    def get_formsets_with_inlines(self, request, obj=None):
        for inline in self.get_inline_instances(request, obj):
            # FILTER THE INLINE FORMSET TO YIELD HERE 
            # For example, given obj.related_instances value
            if obj is not None and obj.related_instances.count() > 0:
                yield inline.get_formset(request, obj), inline

See django doc here.