Django:与用户表的外键关系不验证
考虑以下的django模型
from django.db import models
from django.contrib import auth
class Topic(models.Model):
user = models.ForeignKey('auth.models.User')
name = models.CharField(max_length = NameMaxLength , unique = True)
version_number = models.IntegerField(default = 0)
created_at = models.DateTimeField(auto_now_add = True)
modified_at = models.DateTimeField(auto_now = True)
update_frequency = models.IntegerField()
这个模型即使在安装了auth_user
表之后也无法进行验证。
In [3]: auth.models.User.objects.all()
Out[3]: [<User: admin>]
以上的说法来自django-admin命令行界面
$ python manage.py syncdb
Error: One or more models did not validate:
topic: 'user' has a relation with model auth.models.User, which has either not
been installed or is abstract.
我在ubuntu 11.04上使用django v1.0.4和pinax 0.7.2,并且使用的是sqlite3数据库
以下问题对我帮助不大:
3 个回答
1
我遇到了同样的错误,不过是在不同的情况下。
我把models.py这个文件拆分成了两个文件:
myapp/
models/
__init__.py
foo.py
bar.py
在foo.py里,我有两个模型:
class Foo(models.Model):
attr1 = ... etc
class FooBar(models.Model):
other = ... etc
foo = models.ForeignKey(Foo)
class Meta():
app_label = 'foo'
我通过在Foo模型里也添加Meta来解决这个问题:
class Foo(models.Model):
attr1 = ... etc
class Meta():
app_label = 'foo'
7
我也遇到过同样的问题,
错误信息很明确:你还没有安装用户模型。
Add "django.contrib.auth" to INSTALLED_APPS in your settings.py.
就是这样……希望这能解决你的问题,对我来说是有效的。
33
from django.db import models
from django.contrib.auth.models import User
class Topic(models.Model):
user = models.ForeignKey(User)
使用'auth.User'
也是可以的。这不是Python的库语法,而是Django的ORM(对象关系映射)中的“应用名.模型名”语法。不过,只有在你非常想解决循环依赖的问题时,才应该把模型作为字符串传递。如果你遇到了循环依赖,那你的代码就有问题了。