Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django extend ModelAdmin fieldsets and keep defaults

Tags:

django-admin

is there a nice way to add custom items to a subclassed ModelAdmin fieldset, ie have it keep all the defaults and just some extras.

(i know i could add all the defaults back myself, but was hoping for a nicer way)

like image 588
second Avatar asked Aug 21 '10 12:08

second


1 Answers

You can override get_fieldsets method of ModelAdmin.

The default implementation looks like this:

def get_fieldsets(self, request, obj=None):
    "Hook for specifying fieldsets for the add form."
    if self.declared_fieldsets:
        return self.declared_fieldsets
    form = self.get_form(request, obj)
    fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj))
    return [(None, {'fields': fields})]

So you can override it for example like this:

class MyCustomAdmin(ModelAdmin):
    def get_fieldsets(self, request, obj=None):
        fs = super(MyCustomAdmin, self).get_fieldsets(request, obj)
        # fs now contains [(None, {'fields': fields})], do with it whatever you want
        all_fields = fs[0][1]['fields']
        return fs
like image 112
user920391 Avatar answered Sep 28 '22 02:09

user920391