DeserializationError:'NoneType'对象使用Django loaddata没有属性'_meta' [英] DeserializationError: 'NoneType' object has no attribute '_meta' using Django loaddata

查看:527
本文介绍了DeserializationError:'NoneType'对象使用Django loaddata没有属性'_meta'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的Django应用程序数据库从SQLite迁移到MySQl。



我使用 dump.py dumpdata转储数据 - 自然> hunt.json



然后更新设置以连接到MySQL数据库后,当我 loaddata 使用 manage.py loaddata hunt 它会引发以下错误

 安装夹具'hunt.json'的问题:Traceback(最近的最后一次调用):
文件C:\Python27\lib\site- packages\django\core\management\commands\\ \\ loaddata.p
y,行190,在对象
中的对象中的obj:
文件C:\Python27\lib\site- packages\django\core\\ \\ serializers\json.py,第47行,
在解串器
raise DeserializationError(e)
DeserializationError:'NoneType'对象没有属性'_meta'

以下是我的model.py

 从django.db导入模型

class DJManager(models.Manager):
def get_by_natural_key(self,name):
return self.get(name = name)

class DJ(models.Model):
objects = DJManager()

name = models.CharField(max_length = 50,unique = True)
rank = models .IntegerField()
img = models.ImageField(upload_to ='/ img /',height_field = None,width_field = None)
soundcloud_profile = models.CharField(max_length = 100,blank = True,null =真的)

def natural_key(self):
return(self.name,)

#用于人类可读的对象表示
def __unicode __(self )
return self.name

class Song(models.Model):
song_id = models.IntegerField()
name = models.CharField(max_length = 100 )
title = models.CharField(max_length = 100)
normalized_name = models.CharField(max_length = 100)
artist = models.ForeignKey(DJ,default = None,blank = True,null = True)
artists = models.CharField(max_length = 100)
remixers = models.CharField(max_length = 100,default = None,blank = True,null = True)
release_date = models.DateField()
slug = models.CharField(max_length = 100)
artwork = models.CharField(max_length = 100)
genres = models.CharField(max_length = 50)
duplicate = models.BooleanField(default = False)
vote = models.IntegerField(default = 0)

#用于人类可读的对象表示
def __unicode __(self ):
return self.normalized_name


解决方案

南与测试相冲突。我应该根据 settings.py 中设置 SOUTH_TESTS_MIGRATE = False south.readthedocs.org/en/latest/settings.html#south-tests-migraterel =nofollow> docs


如果这是False,South的测试运行器集成将使用syncdb创建测试
数据库,而不是通过迁移(
默认值)创建。


另外我把所有东西都倾倒在我的装置上。这显然是一个糟糕的方法来与南方进行配合 - 因为它也将南方迁移表转储到固定装置中。所以我不得不创建我的夹具是应用程序的特定
manage.py dumpdata hunt --natural> hunt.json


I am trying to migrate my Django application database from SQLite to MySQl.

I'm dumping the data using manage.py dumpdata --natural > hunt.json

Then after updating the settings to connect to a MySQL database, when I loaddata using manage.py loaddata hunt it throws the following error

Problem installing fixture 'hunt.json': Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\management\commands\loaddata.p
y", line 190, in handle
    for obj in objects:
  File "C:\Python27\lib\site-packages\django\core\serializers\json.py", line 47,
 in Deserializer
    raise DeserializationError(e)
DeserializationError: 'NoneType' object has no attribute '_meta'

Following is my model.py

from django.db import models

class DJManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name=name)

class DJ(models.Model):
    objects = DJManager()

    name = models.CharField(max_length=50, unique=True)
    rank = models.IntegerField()
    img = models.ImageField(upload_to='/img/', height_field=None, width_field=None)
    soundcloud_profile = models.CharField(max_length=100, blank=True, null=True)

    def natural_key(self):
    return (self.name,)

    #for human readable representation of objects
    def __unicode__(self):
        return self.name

class Song(models.Model):
    song_id = models.IntegerField()
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    normalized_name = models.CharField(max_length=100)
    artist = models.ForeignKey(DJ, default=None, blank=True, null=True)
    artists = models.CharField(max_length=100)
    remixers = models.CharField(max_length=100, default=None, blank=True, null=True)
    release_date = models.DateField()
    slug = models.CharField(max_length=100)
    artwork = models.CharField(max_length=100)
    genres = models.CharField(max_length=50)
    duplicate = models.BooleanField(default=False)
    votes = models.IntegerField(default=0)

    #for human readable representation of objects
    def __unicode__(self):
        return self.normalized_name

解决方案

South was conflicting with the test. I was supposed to set SOUTH_TESTS_MIGRATE = False in settings.py as per the docs.

If this is False, South’s test runner integration will make the test database be created using syncdb, rather than via migrations (the default).

Also I was dumping everything into my fixture. This is, apparently, a bad way to do fixtures it with South--because it also dumps the South migration tables into the fixture. So I had to create my fixture to be app-specific manage.py dumpdata hunt --natural > hunt.json

这篇关于DeserializationError:'NoneType'对象使用Django loaddata没有属性'_meta'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆