1👍
I suggest you not use django models in settings.py
,
Generally, after settings.py
loads, django knows what the database config is.
A more idiomatic practice is add some piece of code at the end of settings.py:
from local_settings import *
or just overwrite ADMINS
to the namespace, hardcode ADMINS = (('admin', 'admin@example.com'))
in local_settings,
from local_settings import ADMINS
As you do not want to expose the information to github, add local_settings.py
to your .gitignore
.
As @DJV said,” ADMINS in settings.py and the superusers mean different things (the first are expected to manage the site, while the others to manage the content)”, I do not think ADMINS is so important.
0👍
I wanted this code to run when the server is spun up not at each request and this solution seems to be doing just that. I don’t want to hard code the ADMINS because i like the idea of using django’s admin to manage users and since i am pulling the username and email from the database i don’t have to worry about exposing the user’s info info in the code.
I created a file called admins.py in my application and in init.py imported that file to execute the code. Now when a user is_superuser their info will be added to ADMINS automatically.
myapp.core.init.py
from myapp.core.admins import *
myapp.core.admins.py
from django.conf import settings
from django.contrib.auth.models import User
from django.core.mail import mail_admins
#admins setup: These users will get 500 error emails
admins = User.objects.filter(is_superuser__exact=True)
settings.ADMINS = []
for u in admins:
settings.ADMINS.append((u.username, u.email))
#send test email
if settings.SEND_TEST_EMAIL:
mail_admins('SEVER HAS BEEN STARTED', 'The server has been started @ '+settings.SITE_IP, fail_silently=False)
UPDATE: There is an issue with the above solution. I had to rebuild my database and when i ran syncdb it fails due to a missing user model. I tried moving the import to models.py init but still no luck. Where is the first safe point of entry?? Do i have to create an admins model or create user profiles to extend auth/user and add an is_admin field? –
- [Answer]-Custom Django Login returning 'OK'
- [Answer]-How to raise errors in Django when running Python script?
- [Answer]-Javascript, Ajax and Alert Not Working
- [Answer]-Django Admin Action: 'QuerySet' object has no attribute 'user'
- [Answer]-Django south internal db functions in django 1.7