[Fixed]-Registering all models in an app with admin programmatically including all fields in list_display

1👍

Here it is:

app = apps.get_app_config('school')

for model_name, model in app.models.items():
    model_admin = type(model_name + "Admin", (admin.ModelAdmin, ), {'list_display': tuple([field.name for field in model._meta.fields])})
    admin.site.register(model, model_admin)
👤mehmet

0👍

Apologies as i haven’t checked this. But i think get_all_field_names() method should be able to get this.

I think you might be able to use it in this way: [field.name for field in MyModel._meta.get_fields()]. But this is deprecated from django 1.8. Might work for now but it will be discontinued in future.

Sorry couldn’t comment on your question due to insufficient reputation.

Leave a comment