在创建第一个超级用户之后初始化

时间:2022-11-15 20:03:45

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

我想知道在创建第一个超级用户时是否发送了特定的信号。当我运行syncdb并被要求创建一个超级用户时。在那一点上,我想知道是否发送了任何信号,以便我可以使用信号来进行一些初始化。或者是否有一种方法可以让一段代码在创建第一个超级用户时运行?我希望这段代码在开始时只运行一次。我希望这个问题有意义……最近有人批评我的问题没有道理。我希望这个可以

2 个解决方案

#1


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)

#2


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.

超用户的创建是由post_syncdb信号调用的,您也可以挂接到post_syncdb信号,然后运行额外的代码。

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.

把下面的代码放到管理/__init__。你的一个应用程序的py,确保这个应用程序的路径在djangob下。在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')

#1


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)

#2


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.

超用户的创建是由post_syncdb信号调用的,您也可以挂接到post_syncdb信号,然后运行额外的代码。

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.

把下面的代码放到管理/__init__。你的一个应用程序的py,确保这个应用程序的路径在djangob下。在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')