supermario supermario -4 years ago 141
Python Question

How to use 'User' as foreign key in Django 1.5

I have made a custom profile model which looks like this:

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

class UserProfile(models.Model):
user = models.ForeignKey('User', unique=True)
name = models.CharField(max_length=30)
occupation = models.CharField(max_length=50)
city = models.CharField(max_length=30)
province = models.CharField(max_length=50)
sex = models.CharField(max_length=1)

But when I run syncdb
, I get:

myapp.userprofile: 'user' has a relation with model User, which has
either not been installed or is abstract.

I also tried:

from django.contrib.auth.models import BaseUserManager, AbstractUser

But it gives the same error. Where I'm wrong and how to fix this?

Answer Source

Change this:

user = models.ForeignKey('User', unique=True)

to this:

user = models.ForeignKey(User, unique=True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download