I want to know if there are any specific signals sent when the first superuser is created. Am talking about when I run syncdb
and am asked to create a super user. At that point I want to know if any signals are sent so that I can Use the signal to do some initialization. Or is there a way I can have a piece of code run only at that moment when the first super user is created. I want that piece of code to run only once at the beginning. help pls. I hope this question makes sense... of late I'v been getting criticism that my questions don't make sense. I hope this one does
Asked
Active
Viewed 342 times
2
-
I think the problem with your issue is that you are not telling us why do you need to do what you want. Maybe you have another problem and can be fixed easily without doing this... I'm really interested to know why you need to handle that signal.... – marianobianchi Mar 09 '13 at 22:46
2 Answers
1
def superuser_creation(sender, instance, created, **kwargs):
user = kwargs['instance']
if user.is_superuser:
//do something here
post_save.connect(superuser_creation, sender=User)

catherine
- 22,492
- 12
- 61
- 85
-
Problem is: syncdb didn't finish yet so if you //do something here that involves other tables, you take the risk it won't work. The other solution seems more robust – jhagege Jan 13 '15 at 19:41
1
The creating of superuser is invoked by post_syncdb
signal, you could hook to the post_syncdb
signal as well and run the extra code afterwards.
Put the following code to the management/__init__.py
of one of your app and make sure the path of that app is under the django.contrib.auth
, in settings.INSTALLED_APPS
.
from django.contrib.auth import models as auth_app
from django.db.models import signals
def operate_upon_first_superuser_after_syncdb(app, created_models, verbosity, db, **kwargs):
if auth_app.User in created_models and kwargs.get('interactive', True):
if auth_app.User.objects.filter(is_superuser=True).exists():
# ... extra code here
signals.post_syncdb.connect(operate_upon_first_superuser_after_syncdb,
sender=auth_app, dispatch_uid='operate_upon_first_superuser_after_syncdb')

okm
- 23,575
- 5
- 83
- 90