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

9.4k views Asked by At

I tried running makemigrations and after migrate and I am constantly getting this error:

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

What I was trying to do is Link a UserProfile model to Django's own User Model:

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)

The "Contests" model (as you can see in my installed apps below) uses the User Model as well without any errors.

My Installed apps look like this :

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',
]

My migration file 0001_initial.py is the following:

# -*- 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)),
            ],
        ),
    ]

Other notes:

  1. I use multiple settings files but my installed apps are all in my base setting file so this should not be the issue.

  2. I am using Python Social Auth to create a Pipeline and create the UserProfile. (But this should not have any effect on the creation of the model in the database)

  3. I even dropped the database and recreated it but I still get the same error.

Thanks in advance!!! :)

3

There are 3 answers

0
Tony On BEST ANSWER

I have deleted all migration files from other apps and run makemigrations and migrate again.

Everything Works now.

1
Bhupesh Varshney On

Try running migrations one by one for every model.

This way you can debug the app you are facing problem with

python manage.py migrate appmname

Maybe running migrations one by one may help

0
Paul Bormans On

I experienced a similar issue where i created a custom user model (as written in the documentation before the first migrate).

class User(AbstractUser):
    pass

AUTH_USER_MODEL = 'account.user'

When trying to migrate i ran into "ValueError: Related model 'account.user' cannot be resolved".

It got resolved after i first generated the migration for the new custom user model.

Paul