我正在EC2实例上部署django应用程序,并且具有以下model.py:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
foto = models.CharField(max_length=200, default="static/batman-for-
def __str__(self):
return str(self.user.first_name) + " Profile"
@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()
当我使用django用户登录时,出现以下错误:
ProgrammingError at /admin/login/
relation "profiles_profile" does not exist
LINE 1: ..."."profiles_profile" FROM "profiles_...
来自:
/home/ubuntu/chimpy/profiles/models.py in save_user_profile
instance.profile.save()
我已经查看了postgres数据库,但找不到名为Profile的表,因此我猜想迁移失败,当我运行python manage.py showmigrations时显示:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
,但与其他表(包括配置文件一个)无关。我还要提及的是,当我在终端中创建超级用户时,出现以下错误:
django.db.utils.ProgrammingError: relation "profiles_profile" does not exist
LINE 1: INSERT INTO "profiles_profile" ("user_id", "foto", "exchange...
我在做什么错?谢谢。
编辑:我的settings.py包括以下内容:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
'portfolios',
'django_extensions',
'rest_framework',
'corsheaders',
]
答案 0 :(得分:1)
查看您的showmigrations
命令的输出,看来问题在于您尚未为profiles
应用创建任何迁移。
要解决此问题,请运行:
python manage.py makemigrations profiles
python manage.py migrate
或者,您可以忽略上述命令中的profiles
来迁移所有需要它们的应用。