开发者

No valid model identifier for Django fixture data?

开发者 https://www.devze.com 2023-02-01 12:50 出处:网络
I\'m sure there\'s a simple answer here but I can\'t see it. I\'m trying to load fixtures into my datab开发者_开发知识库ase but no matter what model identifier I use I keep getting the Deserialization

I'm sure there's a simple answer here but I can't see it. I'm trying to load fixtures into my datab开发者_开发知识库ase 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?


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


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


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: avirtue@someschool.edu

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消