Skip to content

Instantly share code, notes, and snippets.

@NotSqrt
Created July 6, 2013 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotSqrt/5940329 to your computer and use it in GitHub Desktop.
Save NotSqrt/5940329 to your computer and use it in GitHub Desktop.
default populating of list_display with all the fields
class CommonAdmin(admin.ModelAdmin):
ordering = ['id']
def __init__(self, *args, **kwargs):
super(CommonAdmin, self).__init__(*args, **kwargs)
self.set_list_display(args[0]) # set fields to display --> all of them
def set_list_display(self, model):
fields = []
for field in model._meta.fields:
if field.column == 'id':
continue
elif field.get_internal_type() in ('ForeignKey', 'OneToOneField') and field.column.endswith('_id'):
fields.append(field.column[:-3]) # remove the '_id' to get the relation name !! crazy !
else:
fields.append(field.column)
self.list_display = ('__unicode__', ) + tuple(fields)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment