Django 夹具数据没有有效的模型标识符?

发布于 2024-10-09 20:07:05 字数 1772 浏览 0 评论 0原文

我确信这里有一个简单的答案,但我看不到它。我正在尝试将装置加载到我的数据库中,但无论我使用什么模型标识符,我都会不断收到 DeserializationError:无效模型标识符:... 错误。

文件结构:

testproject/
    testapp/
        fixtures/
            data.json
        __init__.py
        models.py
        tests.py
        views.py
    sqlite3.db
    __init__.py
    manage.py
    settings.py
    urls.py

由于这是我第一次尝试固定装置,因此我使用 http:// /www.djangoproject.com/documentation/models/fixtures/

from django.db import models
from django.conf import settings

class Article(models.Model):
    headline = models.CharField(max_length=100, default='Default headline')
    pub_date = models.DateTimeField()

    def __unicode__(self):
        return self.headline

    class Meta:
        ordering = ('-pub_date', 'headline')

data.json:

[
    {
        "pk": "3",
        "model": "testapp.article",
        "fields":
        {
            "headline": "Time to reform copyright",
            "pub_date": "2006-06-16 13:00:00"   
        }
    }, 
    {    
        "pk": "2",
        "model": "testapp.article",
        "fields":
        {
            "headline": "Poker has no place on ESPN",
            "pub_date": "2006-06-16 12:00:00"
        }
    }, 
    {    
        "pk": "1", 
        "model": "testapp.article",
        "fields":
        {
            "headline": "Python program becomes self aware",
            "pub_date": "2006-06-16 11:00:00"
        }
    }
]

我尝试过 testapp.articletestproject.articletestproject.testapp.article 并且它们都抛出相同的错误。我正在使用 Python 2.6 运行 1.2.4 并使用 loaddata 而不是syncdb。有什么想法吗?

I'm sure there's a simple answer here but I can't see it. I'm trying to load fixtures into my database but no matter what model identifier I use I keep getting the DeserializationError: invalid model identifier:... error.

File structure:

testproject/
    testapp/
        fixtures/
            data.json
        __init__.py
        models.py
        tests.py
        views.py
    sqlite3.db
    __init__.py
    manage.py
    settings.py
    urls.py

Since this is my first go at fixtures, I'm using the model from http://www.djangoproject.com/documentation/models/fixtures/:

from django.db import models
from django.conf import settings

class Article(models.Model):
    headline = models.CharField(max_length=100, default='Default headline')
    pub_date = models.DateTimeField()

    def __unicode__(self):
        return self.headline

    class Meta:
        ordering = ('-pub_date', 'headline')

data.json:

[
    {
        "pk": "3",
        "model": "testapp.article",
        "fields":
        {
            "headline": "Time to reform copyright",
            "pub_date": "2006-06-16 13:00:00"   
        }
    }, 
    {    
        "pk": "2",
        "model": "testapp.article",
        "fields":
        {
            "headline": "Poker has no place on ESPN",
            "pub_date": "2006-06-16 12:00:00"
        }
    }, 
    {    
        "pk": "1", 
        "model": "testapp.article",
        "fields":
        {
            "headline": "Python program becomes self aware",
            "pub_date": "2006-06-16 11:00:00"
        }
    }
]

I've tried testapp.article, testproject.article, testproject.testapp.article and they all throw the same error. I'm running 1.2.4 with Python 2.6 and using loaddata rather than syncdb. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

心安伴我暖 2024-10-16 20:07:05

你的 data.json 文件没问题,我已经尝试过了,它有效。

您确定您的数据库与您的模型同步吗?

你运行什么来加载文件?

正如 Luc 建议的那样,将“manage.py dumpdata testapp”输出与您的文件进行比较

your data.json file is fine, I have tried it and it works.

are you sure your db is synced with your models?

what do you run to load the file?

as Luc suggested, compare the "manage.py dumpdata testapp" output with your file

み青杉依旧 2024-10-16 20:07:05

尝试检查settings.py,就我而言,我只是忘记在 INSTALLED_APPS 中添加应用程序

Try check settings.py, in my case i just forget add app in INSTALLED_APPS

猫性小仙女 2024-10-16 20:07:05

我不确定这是否有帮助,但我目前正在查看我编写的一些装置,并且我的所有模型标识符都已正确设置。

这是我的用户帐户固定装置中的示例,但请注意它是在 YAML 中。

- model: auth.User
  pk: 4
  fields:
    username: avirtue
    first_name: Aurora
    last_name: Virtue
    is_active: true
    is_superuser: false
    is_staff: false
    password: sha1$90431$9347d343e94122f94f9f02988f026a76d339ab02
    email: [email protected]

- model: users.UserProfile
  pk: 4
  fields:
    user: 4
    school_id: 420985
    professor: false

该文件位于 users/fixtures/ 文件夹下的一个文件中(也就是说,有一个应用程序 users,并且该文件位于该应用程序的 Fixtures 文件夹中)。

正如您所看到的,这些模型实际上来自两个不同的位置。我使用的第二个来自同一个应用程序,并定义了一个UserProfile。第一个实际上来自 django.contrib.auth 模块,该项目使用该模块进行身份验证。

I'm not sure if this will even help at all, but I'm currently looking at some fixtures I have written and all my model identifiers are properly cased.

Here's an example from my user accounts fixture, but note that it's in YAML.

- model: auth.User
  pk: 4
  fields:
    username: avirtue
    first_name: Aurora
    last_name: Virtue
    is_active: true
    is_superuser: false
    is_staff: false
    password: sha1$90431$9347d343e94122f94f9f02988f026a76d339ab02
    email: [email protected]

- model: users.UserProfile
  pk: 4
  fields:
    user: 4
    school_id: 420985
    professor: false

This is in a file under the folder users/fixtures/ (that is, there is an application users, and this file is located in that application's fixtures folder).

As you can see, the models are actually coming from two different locations. The second one I use is from the same application and defines a UserProfile. The first one is actually from the django.contrib.auth module, which the project uses for authentication.

回忆凄美了谁 2024-10-16 20:07:05

我多次遇到同样的错误“无效的模型标识符”,我总是意识到它要么使用了错误的应用程序名称,要么应用程序名称拼写错误。即“model”:“testapp.article”,testapp 要么拼写错误,要么需要一个不同的应用程序名称,而不是上述情况下的 testapp。

I have had this same error "Invalid model identifier" several times and what I always realized is that it either am using a wrong app name or the app name is wrongly spelled. That is "model": "testapp.article", the testapp is either wrongly spelled or expect a different app name not testapp as of the case above.

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