'Profile'モデルで私の 'User'モデルを拡張したいと思います。これを容易にするために、次のモデルを作成しました。私はリンクされた 'プロフィール'モデルを新しい「ユーザー」モデルごとに自動的に作成したいと思っていました。私は以下のソリューションに付属しているインターネット上のstackoverflow /研究(simpleisbetterthancomplex)上のいくつかのコメントに基づいて:Django OneToOneField post_save cantコールintオブジェクト
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()
しかし、私はこの(かどうかのユニットテストや「スーパーユーザを作成する」スルーを実行するたびに - 私は何のVIEを持っていません
TypeError: 'int' object is not callable
は、ここでは誰もが私が間違っているのか知ってますか?A:まだ、私は、私は次のエラーを取得する)TDDを実践していますか
編集:私はcreated_user_profile
とsave_user_profile
Profile
モデルの両方の外側を保つ「スーパーユーザを作成」コマンドの後に
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
完全なトレースバックを提供しなければわかりません。 –
応答をありがとう、私は、 'スーパーユーザーを作成'コマンドの後に発生するエラーメッセージの完全なトレースバックを追加しました。 – Jasper