我尝试运行 makemigrations 并在迁移后不断收到此错误:
ValueError: Related model 'users.UserProfile' cannot be resolved
我试图做的是将 UserProfile 模型链接到 Django 自己的 User 模型:
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)),
],
),
]
其他说明:
我使用多个设置文件,但我安装的应用程序都在我的基本设置文件中,所以这应该不是问题。
我正在使用 Python Social Auth 创建管道并创建 UserProfile。 (但这应该不会对数据库中模型的创建产生任何影响)
我什至删除了数据库并重新创建了它,但我仍然遇到同样的错误。
提前致谢!!! :)
最佳答案
我已从其他应用程序中删除所有迁移文件并运行 makemigrations 并再次迁移。
现在一切正常。
关于python - Django:无法解析相关模型 'users.UserProfile',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41398949/