Login

Remove named field from fieldsets

Author:
dokterbob
Posted:
November 17, 2009
Language:
Python
Version:
1.1
Tags:
admin fieldset form field fieldsets
Score:
0 (after 0 ratings)

This snipped removes a specific fields from the fieldsets. This is very useful to leave a field 'out' in the admin, likewise:

def get_fieldsets(self, request, obj=None):
    fieldsets = super(BlaModelAdmin, self).get_fieldsets(request, obj)

    if not request.user.has_perm('change_blah'):
        remove_from_fieldsets(fieldsets, ('blah',))
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Django snippet 1803: http://djangosnippets.org/snippets/1803/

import logging

logger = logging.getLogger(__name__)


def remove_from_fieldsets(fieldsets, fields):
    for fieldset in fieldsets:
        for field in fields:
            if field in fieldset[1]['fields']:
                logging.debug("'%s' field found in %s, hiding." % (field, fieldset[1]['fields']))
                newfields = []
                for myfield in fieldset[1]['fields']:
                    if not myfield in fields:
                        newfields.append(myfield)
                        
                fieldset[1]['fields'] = tuple(newfields)
                logger.debug('Setting newfields to: %s' % newfields)
            
                break

More like this

  1. Initial values for new items in admin by RommeDeSerieux 5 years, 8 months ago
  2. ExtendibleModelAdminMixin by dokterbob 5 years, 6 months ago
  3. Fieldsets for Views by Nad 5 years, 4 months ago
  4. DRY Fieldsets by DrMeers 5 years, 11 months ago
  5. Memento by manelvf 4 years, 4 months ago

Comments

Please login first before commenting.