如何在Django 1.5中使用“User”作为外键 [英] How to use 'User' as foreign key in Django 1.5

查看:359
本文介绍了如何在Django 1.5中使用“User”作为外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个自定义个人资料模型,如下所示:

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)

但是当我运行 manage.py syncdb ,我得到:

But when I run manage.py syncdb, I get:


myapp.userprofile:'user'与模型用户,
尚未安装或抽象。

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

我也尝试过:

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

但它给出了相同的错误。我在哪里错了,如何解决这个问题?

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

推荐答案

更改:

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

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

这篇关于如何在Django 1.5中使用“User”作为外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆