Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting "admin.py" of a Django project?

Tags:

python

django

I'm trying to split admin.py of a Django project into separate files but failed.

There's no information I can find from google about how to split it, so I have to try myself. Here's what I tried:

  1. make a directory named separated_admins and put an empty __init__.py in it
  2. create files in separated_admins directory, something like this:

    # file my_app/seperated_admins/Some_Model_admin.py
    
    from my_app.models import Some_Model
    from django.contrib import admin
    
    admin.site.register(Some_Model)
    
  3. in admin.py, I added lines like:

    from my_app.seperated_admins import *
    

But I didn't see Some_Model in my admin site. Is my solution right? How can I fix this?

like image 393
yegle Avatar asked Aug 19 '11 07:08

yegle


People also ask

How do I change the administrator name in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.

Can I use Django admin in production?

your company should follow a least access principle policy; so yes: only select people should have access. Django has basic auditing capability via signals and displayed in the admin out of the box. You can build on top of this.

How can I remove extra's from Django admin panel?

You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Literally saved my life!


1 Answers

The admin is just a python module. So the right way of splitting it would be as follows:

  • Create a folder called admin instead of admin.py
  • Use multiple files within the admin folder, like your Some_Model_admin.py
  • Create a __init__.py in the admin folder and import * all the files into it.
  • You might also want to include an __all__ to provide a clean interface.
like image 88
lprsd Avatar answered Sep 22 '22 15:09

lprsd