Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register multiple models with the admin?

If I want to register my models with the admin I have to do this like this:

#admin.py admin.site.register(models.About) 

But with multiple models you can't do something like this:

models = (models.Project, models.Client, models.About) for m in models:    admin.site.register(m) 

First of all: why not!? Secondly: imagine one has a lot of models which all should be accessible from the admin interface. How do you do that in a generic way?

like image 951
LarsVegas Avatar asked Oct 24 '12 07:10

LarsVegas


People also ask

How do I add an administrator to a model?

You can check the admin.py file in your project app directory. If it is not there, just create one. Edit admin.py and add below lines of code to register model for admin dashboard. Here, we are showing all the model fields in the admin site.

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

Is Django's admin interface customizable if yes then how?

The Django framework comes with a powerful administrative tool called admin. You can use it out of the box to quickly add, delete, or edit any database model from a web interface. But with a little extra code, you can customize the Django admin to take your admin capabilities to the next level.


1 Answers

admin.site.register has this definition in the library:

def register(self, model_or_iterable, admin_class=None, **options): 

so models to be registered can be a single model or iterable object so just use this:

myModels = [models.Project, models.Client, models.About]  # iterable list admin.site.register(myModels) 

I tested this in my site and works perfectly fine.

like image 126
Rohit Avatar answered Oct 14 '22 21:10

Rohit