2

我正在尝试为创建的每个用户帐户创建一个配置文件。我正在使用 Django 1.4 和 python 2.7。

我有我的Profile模型课:

class Profile(models.Model): 
   ... profile fields ...
   user = models.OneToOneField(User)

然后我使用 post save 信号来创建Profile

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
        #Profile(user=instance).save()

post_save.connect(create_user_profile, sender=User, dispatch_uid="accounts.models")

如果我使用Profile.objects.create(user=instance)我得到错误:

save() takes no arguments (3 given)

但如果我使用Profile(user=instance).save()我会得到错误:

save() takes no arguments (1 given)

我基本上直接从 django 文档中复制了这个。

我真的不确定这里出了什么问题,所以任何帮助都将不胜感激。

编辑

问题已在评论中解决:

我有 defsave():而不是def save(self, *args, **kwargs):

4

1 回答 1

0

save()self作为默认参数。所以如果没有显式传递,Django 会自动传递self默认参数。

所以你需要self明确地传递给save(), iedef save(self)然后save()在任何地方调用,你就不会再得到错误了。

于 2014-06-10T06:59:04.437 回答