'Validate that fieldsets is properly formatted and doesn\'t contain duplicates.'

def validate_fieldsets(self, cls, model):

    from django.contrib.admin.options import flatten_fieldsets
    if cls.fieldsets:
        check_isseq(cls, 'fieldsets', cls.fieldsets)
        for (idx, fieldset) in enumerate(cls.fieldsets):
            check_isseq(cls, ('fieldsets[%d]' % idx), fieldset)
            if (len(fieldset) != 2):
                raise ImproperlyConfigured(("'%s.fieldsets[%d]'    does    not    have    exactly    two    elements." % (cls.__name__, idx)))
            check_isdict(cls, ('fieldsets[%d][1]' % idx), fieldset[1])
            if ('fields' not in fieldset[1]):
                raise ImproperlyConfigured(("'fields'    key    is    required    in    %s.fieldsets[%d][1]    field    options    dict." % (cls.__name__, idx)))
            self.check_field_spec(cls, model, fieldset[1]['fields'], ("fieldsets[%d][1]['fields']" % idx))
        flattened_fieldsets = flatten_fieldsets(cls.fieldsets)
        if (len(flattened_fieldsets) > len(set(flattened_fieldsets))):
            raise ImproperlyConfigured(('There    are    duplicate    field(s)    in    %s.fieldsets' % cls.__name__))
