Django 夹具数据没有有效的模型标识符?
我确信这里有一个简单的答案,但我看不到它。我正在尝试将装置加载到我的数据库中,但无论我使用什么模型标识符,我都会不断收到 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.article
、testproject.article
、testproject.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的 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
尝试检查settings.py,就我而言,我只是忘记在 INSTALLED_APPS 中添加应用程序
Try check settings.py, in my case i just forget add app in INSTALLED_APPS
我不确定这是否有帮助,但我目前正在查看我编写的一些装置,并且我的所有模型标识符都已正确设置。
这是我的用户帐户固定装置中的示例,但请注意它是在 YAML 中。
该文件位于 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.
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 thedjango.contrib.auth
module, which the project uses for authentication.我多次遇到同样的错误“无效的模型标识符”,我总是意识到它要么使用了错误的应用程序名称,要么应用程序名称拼写错误。即“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.