我想用'Profile'模型扩展我的'User'模型。为方便起见,我创建了以下模型。我想让每个新的“用户”模型自动创建链接的“配置文件”模型。基于对互联网上的stackoverflow /研究的一些评论(simpleisbetterthancomplex),我提出了以下解决方案:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
#Pushup related stats
total_pushups = models.IntegerField(default=0)
best_consecutive = models.IntegerField(default=0)
week_streak = models.IntegerField(default=0)
save = models.IntegerField(default=000)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
然而,每次我运行它(无论是通过单元测试还是'创建超级用户' - 我还没有竞争,我正在练习TDD)我收到以下错误:
TypeError: 'int' object is not callable
有谁在这里知道我做错了什么?
编辑:我已经包含了“创建超级用户”命令后显示的错误消息的回溯
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 170, in create_superuser
return self._create_user(username, email, password, **extra_fields)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 153, in _create_user
user.save(using=self._db)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/base.py", line 847, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in send
for receiver in self._live_receivers(sender)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
for receiver in self._live_receivers(sender)
File "/home/jasper/PycharmProjects/PushUpTuneUp/user_profiles/models.py", line 20, in create_user_profile
Profile.objects.create(user=instance)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
TypeError: 'int' object is not callable
答案 0 :(得分:1)
您已经定义了一个名为save
的字段,该字段隐藏了实际的save
方法。将其重命名为其他内容。
(与你的问题无关,但你当然不应该有两个接收器。我不明白为什么你需要第二个接收器,但即使你确实需要这个逻辑,你应该把它放进去与第一个。)