Django:相关模型'users.UserProfile'无法解析

时间:2016-12-30 15:50:37

标签: python django postgresql django-models django-1.10

我尝试运行makemigrations并在迁移后我不断收到此错误:

ValueError: Related model 'users.UserProfile' cannot be resolved

我想要做的是将 UserProfile 模型链接到Django自己的用户模型:

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    website = models.URLField(blank=True)
    bio = models.CharField(max_length=250, blank=True)
    full_name = models.CharField(max_length=250, blank=True)

比赛”模式(如下面我已安装的应用中所示)也使用用户模型,没有任何错误。

我的已安装的应用如下所示:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',
    'social.apps.django_app.default',
    'crispy_forms',
    'pages',
    'contests',
]

我的迁移文件 0001_initial.py 如下:

# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-30 15:45
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='UserProfile',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('website', models.URLField(blank=True)),
                ('bio', models.CharField(blank=True, max_length=250)),
                ('full_name', models.CharField(blank=True, max_length=250)),
                ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]

其他说明:

  1. 我使用多个设置文件,但我安装的应用程序都在我的基本设置文件中,所以这不应该是问题。

  2. 我使用Python Social Auth创建管道并创建UserProfile。 (但这不会对数据库中模型的创建产生任何影响)

  3. 我甚至删除了数据库并重新创建了它,但我仍然遇到同样的错误。

  4. 提前致谢!!! :)

4 个答案:

答案 0 :(得分:7)

我已从其他应用中删除了所有迁移文件并运行makemigrations并再次迁移。

现在一切正常。

答案 1 :(得分:1)

尝试为每个模型一个接一个地运行迁移。

通过这种方式,您可以调试遇到问题的app

python manage.py migrate appmname

也许逐个运行迁移会有所帮助

答案 2 :(得分:0)

我遇到了类似的问题,我创建了一个自定义用户模型(如第一次迁移之前在文档中所写)。

private static void getDbConn()

当尝试迁移时,我遇到了“ ValueError:无法解析相关模型'account.user'”。

在我首次为新的自定义用户模型生成迁移后,该问题得到解决。

保罗

答案 3 :(得分:0)

您可以使用 cmd-shift-p 或单击查看 -> 命令面板并运行命令 Python:选择解释器来修复它。只需在此处添加 virtualenv 目录即可享受。

相关问题