Created
July 6, 2013 16:07
-
-
Save NotSqrt/5940329 to your computer and use it in GitHub Desktop.
default populating of list_display with all the fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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