Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django用户profil自动创建在管理站点上不起作用_Django_Instance - Fatal编程技术网

Django用户profil自动创建在管理站点上不起作用

Django用户profil自动创建在管理站点上不起作用,django,instance,Django,Instance,我认为这是Django管理上的一个bug,当您在管理站点上添加一个用户,并且您的模型上有一个自动创建profil时,由于创建profil的实例出现问题,站点崩溃: def create_user_profile(sender, instance, created, **kwargs) : if created : data = Profile.objects.get_or_create(user = instance) profile = data[0]

我认为这是Django管理上的一个bug,当您在管理站点上添加一个用户,并且您的模型上有一个自动创建profil时,由于创建profil的实例出现问题,站点崩溃:

def create_user_profile(sender, instance, created, **kwargs) :

    if created :
        data = Profile.objects.get_or_create(user = instance)
        profile = data[0]

        # Initialisation du profil
        profile.user = instance
        profile.credits = 0
        profile.score = 0

        profile.save()

post_save.connect(create_user_profile, sender = User,
                  dispatch_uid='create_user_profile')
在管理站点上,实例不是用户,实例是管理员用户


解决方案是在管理站点上创建用户时删除此代码,其他解决方案如何?这真烦人

尝试注释掉行
profile.user=instance
。创建或检索的配置文件对象已由
get\u或
设置了用户,请尝试注释掉行
profile.user=instance
。已创建或检索的配置文件对象的用户设置为
get\u或\u create

站点如何“崩溃”?你能回溯一下吗?网站是如何“崩溃”的?你能回溯一下吗?对不起,我已经完成了我的代码。问题是,获取或创建获取我的profile(因为我登录了管理站点,所以是管理profile),但是,它应该获取我创建的用户profile…(1062,“密钥“user\u id”的重复条目“9”)
get\u或\u create
不应该导致该错误-你确定该代码导致了该错误吗?不,我的意思是我确定这行是问题所在,当然,如果我删除了这个问题,就没有问题了(哈哈:),但它不能解决它。我想保留这段代码来自动创建用户的profil,但是,当我在管理站点中创建用户时,它不起作用,我认为问题与此线程相同:管理站点在模型之前保存profil…我已经通过覆盖profil的保存方法解决了我的问题。就像他们在《对不起》中说的,我已经完成了我的代码。问题是,获取或创建获取我的profile(因为我登录了管理站点,所以是管理profile),但是,它应该获取我创建的用户profile…(1062,“密钥“user\u id”的重复条目“9”)
get\u或\u create
不应该导致该错误-你确定该代码导致了该错误吗?不,我的意思是我确定这行是问题所在,当然,如果我删除了这个问题,就没有问题了(哈哈:),但它不能解决它。我想保留这段代码来自动创建用户的profil,但是,当我在管理站点中创建用户时,它不起作用,我认为问题与此线程相同:管理站点在模型之前保存profil…我已经通过覆盖profil的保存方法解决了我的问题。就像他们在书中说的