Django:与用户表的外键关系未验证

发布于 2024-12-02 23:55:36 字数 1457 浏览 1 评论 0原文

考虑以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小沫ゞ 2024-12-09 23:55:36
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 的“app.model”语法。但是,如果您拼命尝试解决循环依赖关系,则只应将模型作为字符串传递。如果你有循环依赖,你的代码就会失效。

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

class Topic(models.Model):
    user = models.ForeignKey(User) 

'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.

人心善变 2024-12-09 23:55:36

即使我遇到了同样的问题,

错误消息很明确:您尚未安装用户模型。

Add "django.contrib.auth" to INSTALLED_APPS in your settings.py.

这一切......希望它能解决这个问题,对我来说效果很好。

Even I faced same issue,

The error message is clear: you haven't installed the User model.

Add "django.contrib.auth" to INSTALLED_APPS in your settings.py.

That all..Hope it will solve this issue, worked fine for me.

奶气 2024-12-09 23:55:36

我有同样的错误,但情况不同。
我将 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'

I had the same error, but in different situation.
I splitted the models.py in two files:

myapp/
    models/
        __init__.py
        foo.py
        bar.py

In foo.py I had two models:

class Foo(models.Model):
    attr1 = ... etc

class FooBar(models.Model):
    other = ... etc
    foo = models.ForeignKey(Foo)

    class Meta():
        app_label = 'foo'

I solved adding Meta also in Foo model:

class Foo(models.Model):
    attr1 = ... etc

    class Meta():
        app_label = 'foo'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文