Making inlines conditional in the Django admin

后端 未结 8 2330
长发绾君心
长发绾君心 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:46

    I think the easiest way to hack this is to call your custom funciton in get_fields, or get_fieldsets and so on, just set self.inlines in a custom function.

    class XXXAdmin(admin.ModelAdmin):
        def set_inlines(self, request, obj):
            """ hack inlines models according current request.user or obj """
            self.inlines = []
            if request.user.is_superuser or request.user is obj.recorder:
                self.inlines = [AbcInline, ]
    
        def get_fields(self, request, obj=None):
            self.set_inlines(request, obj)  # NOTICE this line
            super(XXXAdmin, self).get_fields(request, obj)
    

提交回复
热议问题