admin.py for project, not app

2024/10/2 12:17:19

How can I specify a project level admin.py?

I asked this question some time ago and was just awarded the Tumbleweed award because of the lack of activity on the question! >_<

Project:

  • settings.py
  • admin.py (This is what I am trying to get to work)
  • ...
  • App
    • admin.py (I know how to do this)

For example, admin.autodiscover() is typically put in the project level urls.py (yes, it will be automatically included in 1.7)

I would like to move this, and the following, into their own admin.py file:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import UserUserAdmin.list_display = ('email', 'first_name', 'last_name', 'is_active')
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.autodiscover()

I tried doing just that, making an admin.py file and adding the code into it. Didn't work.

I tried adding a project level folder called admin, added an __init__.py and threw admin.py into the admin folder. Didn't work

Further I tried adding this admin folder to INSTALLED_APPS in settings.py. No luck.

Answer

From the admin.autodiscover doc string:

Auto-discover INSTALLED_APPS admin.py modules and fail silently when not present. This forces an import on them to register any admin bits theymay want.

And here is the complete function very well documented:

def autodiscover():"""Auto-discover INSTALLED_APPS admin.py modules and fail silently whennot present. This forces an import on them to register any admin bits theymay want."""import copyfrom django.conf import settingsfrom django.utils.importlib import import_modulefrom django.utils.module_loading import module_has_submodulefor app in settings.INSTALLED_APPS:mod = import_module(app)# Attempt to import the app's admin module.try:before_import_registry = copy.copy(site._registry)import_module('%s.admin' % app)except:# Reset the model registry to the state before the last import as# this import will have to reoccur on the next request and this# could raise NotRegistered and AlreadyRegistered exceptions# (see #8245).site._registry = before_import_registry# Decide whether to bubble up this error. If the app just# doesn't have an admin module, we can ignore the error# attempting to import it, otherwise we want it to bubble up.if module_has_submodule(mod, 'admin'):raise

So the only thing autodiscover does for you is look for some module called admin.py inside installed app directories, hence you can put your admin.py where you want just make sure you import it, this make the code in it (registration of models ect..) get executed.

IMPORTANT: I'm not sure the correct moment for importing your custom-path admin.py. But it's sure you have to import it after load all the related apps.

https://en.xdnf.cn/q/70848.html

Related Q&A

Python Socket Receive/Send Multi-threading

I am writing a Python program where in the main thread I am continuously (in a loop) receiving data through a TCP socket, using the recv function. In a callback function, I am sending data through the …

numpy array2string applied on huge array, skips central values, ( ... in the middle )

I have array of size (3, 3, 19, 19), which I applied flatten to get array of size 3249.I had to write these values to file along with some other data, so I did following to get the array in string.np.a…

save password as salted hash in mongodb in users collection using python/bcrypt

I want to generate a salted password hash and store it in MongoDB collection called users, like this:users_doc = { "username": "James","password": "<salted_hash_pa…

Get the min of [0, x] element wise for a column

I need to compute a column where the value is the result of a vectorized operation over other columns: df["new_col"] = df["col1"] - min(0,df["col2"])It turned out, however…

Virtual column in QTableView?

Im started to learning Qt4 Model/View Programming and I have beginner question.I have simple application which show sqlite table in QTableView:class Model(QtSql.QSqlTableModel):def __init__(self, paren…

Python httplib2, AttributeError: set object has no attribute items

Im playing with the Python library httplib2. The following is my code. import urllib.parse import httplib2httplib2.debuglevel = 1http = httplib2.Http()url = "http://login.sina.com.cn/hd/signin.php…

Atom IDE autocomplete-python not working

I have just installed the Atom IDE and the package autocomplete-python (on Windows). But the package is not working. Do I have to make any setting changes? (I have disabled autocomplete-plus and autoc…

Multiple instances of a class being overwritten at the same time? (Python)

Heres a very simple code I made to demonstrate the problem Im encountering. Whats happening here is that Im creating two different instances of the same class but changing an attribute of one will cha…

how to store binary file recieved by Flask into postgres

I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route(/upload, methods=[POST]) def upload_file():def allowed_file(f):return Truefile …

How can I kill a single shot QtCore.QTimer in PyQt4?

So, in my application, I create a single QtCore.QTimer object and then call the singleShot method on it to evoke a function after say 60 secs. Now, at any given point in time, if I would need to call t…