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 shell
$ 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 数据库
以下问题没有多大帮助:
Consider the following django model
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()
This model does not validate even after installing the auth_user
table.
In [3]: auth.models.User.objects.all()
Out[3]: [<User: admin>]
The above statement is from django-admin shell
$ 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.
I am using django v1.0.4 with pinax 0.7.2 on ubuntu 11.04 , with sqlite3 database
The following Questions did not help much:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
'auth.User'
也会起作用。这不是 Python 的库语法,而是 Django ORM 的“app.model”语法。但是,如果您拼命尝试解决循环依赖关系,则只应将模型作为字符串传递。如果你有循环依赖,你的代码就会失效。'auth.User'
would have worked, too. It's not Python's library syntax, it's the Django ORM's "app.model" syntax. But you should only pass the model as a string if you're desperately trying to solve a circular dependency. And if you have a circular dependency, your code is eff'd.即使我遇到了同样的问题,
错误消息很明确:您尚未安装用户模型。
这一切......希望它能解决这个问题,对我来说效果很好。
Even I faced same issue,
The error message is clear: you haven't installed the User model.
That all..Hope it will solve this issue, worked fine for me.
我有同样的错误,但情况不同。
我将 models.py 分成两个文件:
在 foo.py 中,我有两个模型:
我解决了在 Foo 模型中添加 Meta 的问题:
I had the same error, but in different situation.
I splitted the models.py in two files:
In foo.py I had two models:
I solved adding Meta also in Foo model: