'Django Admin site - where to put admin.py

I inherited a Django application at work and I am attempting to make some improvements. The application is working without issue however there is some maintenance work that is being done directly in the database tables. I would like to setup the Admin site to avoid having to directly edit the database table(s) content.

I have minimal experience with Django and have been thru a lot of the tutorials however this application structure doesn't really match any I have seen before.

The application is built using Django 3.2.9 (from the requirements.txt) The production version is hosted in GCP on App Engine with a MySQL CloudSQL instance. I am currently testing on a local machine via manage.py runserver(using a local MySQL instance.) At some point I will be updating versions of Django.

The code is structured like this

project/  
|---manage.py  
|---requirements.txt  
+---project_app/  
|----|----settings.py  
|----|----urls.py  
+---app1/  
|---+---models/  
|----|---+---app1/  
|----|----|----|----app1.py  
|---+---urls/  
|----|---+---app1/  
|----|----|----|----routing.py  
|---+---views/  
|----|---+---app1/  
|----|----|----|----view.py  
+---app2  
|---+---similar structure to app1  
+---app3  
|---+---similar structure to app1  
+---app4  
|---+---similar structure to app1  

My question is, which folder would I create the admin.py inside the app1 application in order to import the models for the Admin site to manage?

The Admin site is loading and shows the Users and Groups tables (along with the Python Social Auth and Auth Tokens tables) in the list.

There is very little documentation I could find about how the Admin site locates admin.py files. I have also tried using Procmon (Windows) to see if it attempts to find admin.py in any folders. Procmon is not seeing any reads on any admin.py file except under the site-packages in the venv.

Thanks for any help



Solution 1:[1]

Update: Was able to solve this

TL;DR the admin.py goes into same folder you have the "import admin" line in.
Adding

from django.contrib import admin

into app.py in app1 and having an admin.py in the same folder and making admin configuration does work

project/  
|---manage.py  
|---requirements.txt  
+---project_app/  
|----|----settings.py  
|----|----urls.py  
+---app1/  
|----app.py  
|----admin.py  
|---+---models/  
|----|----models_file.py
|---+---urls/  
|----|---+---app1/  
|----|----|----|----routing.py  
|---+---views/  
|----|---+---app1/  
|----|----|----|----view.py  
+---app2  
|---+---similar structure to app1  

Some additional things

  1. The order of INSTALLED_APPS matters if you want to customize the admin site models
    I wanted to remove the social_django models from the admin and that required putting social_django and social_core above app1 in INSTALLED_APPS

  2. This application is using Django REST Framework with the rest_framework_simplejwt package which includes a Tokens model and this shows by default in the admin. Make sure the DRF and DRF jwt is before app1 in the INSTALLED_APPS than it can be unregistered in admin.py

End result is a admin.py file that looks like this

from django.contrib import admin

from django.contrib.auth.models import User, Group
from social_django.models import Association, Nonce, UserSocialAuth
from rest_framework.authtoken.models import TokenProxy
# My app1 models Imports

# Remove Django REST Framework models from admin.  
admin.site.unregister(TokenProxy)

# Remove Default user and group models provided by Django
admin.site.unregister(User)
admin.site.unregister(Group)

# Remove Social Auth models from social-auth-app-django package
admin.site.unregister(Association)
admin.site.unregister(Nonce)
admin.site.unregister(UserSocialAuth)

# My app1 models site.unregister

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aeatthompson