def add_dynamic_value(instance, attr, value, index=None, cache={}): """ Dynamically insert or append (in a thread safe way) a value to an admin option, e.g. *list_display* or *list_filter*. Arguments: - instance: an *admin.ModelAdmin* instance - attr: a string representing the admin option to change (e.g. *list_display*) - value: the value to append or insert - index: the position where to insert the new value (None: append) """ key = '%d%s' % (id(instance), attr) if key not in cache: cache[key] = getattr(instance, attr) new_value = list(cache[key]) if index: if new_value[0] == 'action_checkbox': index += 1 new_value.insert(index, value) else: new_value.append(value) setattr(instance, attr, new_value)