Making inlines conditional in the Django admin

后端 未结 8 2349
长发绾君心
长发绾君心 2021-02-07 21:45

I have a model that I want staff to be able to edit up to the date for the event. Like this:

class ThingAdmin(admin.ModelAdmin):
    model = Thing

    if obj.da         


        
8条回答
  •  春和景丽
    2021-02-07 22:39

    I had a complex case where the solutions I tried failed in unexpected ways (problems with readonly fields in inlines). This is the most clear and failsafe way I've found:

    class MyAdmin(admin.ModelAdmin):
    
        def add_view(self, request, form_url='', extra_context=None):
            self.inlines = [InlineA, InlineC]
            return super(MyAdmin, self).add_view(request, form_url, extra_context)
    
        def change_view(self, request, object_id, form_url='', extra_context=None):
            self.inlines = [InlineB, InlineC, InlineD]
            return super(MyAdmin, self).change_view(request, object_id, form_url, extra_context)
    

    This is working in Django 1.4.x.

提交回复
热议问题