我正在尝试为每个创建的用户帐户创建一个配置文件。我正在使用 Django 1.4 和 python 2.7。
我有我的 Profile 模型类:
class Profile(models.Model):
... profile fields ...
user = models.OneToOneField(User)
然后我使用后保存信号创建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 文档中复制的。
我真的不确定这里出了什么问题,所以我们将不胜感激。
编辑
问题在评论中解决了:
I had def
save():instead ofdef save(self, *args, **kwargs):
请您参考如下方法:
save() 将 self 作为默认参数。因此,如果没有显式传递,Django 将自动传递 self 默认参数。
所以你需要将 self 显式传递给 save(),即 def save(self) 然后调用 save( ) 任何地方,你不会再得到错误。
