我尝试运行 makemigrations 并在 migrate 之后不断收到此错误:
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)),
],
),
]
其他注意事项:
我使用多个设置文件,但我安装的应用程序都在我的基本设置文件中,所以这应该不是问题。
我正在使用 Python Social Auth 创建管道并创建 UserProfile。(但这应该不会对数据库中模型的创建产生任何影响)
我什至删除了数据库并重新创建了它,但我仍然得到同样的错误。
提前致谢!!!:)