Django:DoesNotExist:匹配查询不存在

时间:2012-06-04 18:22:25

标签: mysql django django-models dumpdata

我正在将django(v1.1)项目从mysql移动到postgresql(有趣!),目前我正在传输所有数据。我试图使用manage.py dumpdata选项,但我们使用的服务器相当陈旧,需要很长时间(似乎想要将所有内容加载到内存中)。我想出了一个小脚本来处理每个应用程序,然后是它下面的每个模型,简化版本如下所示:

 def dumpJson(model, outfile):  
    try:
            data = serializers.serialize("json", model.objects.all(),indent=4)                
    except model.DoesNotExist:
            print model.__name__ , "Does not Exist (not Object)"
    except ObjectDoesNotExist:
            print model.__name__ , "Issue with the data, not sure what...."

            data = serializers.serialize("json", model.objects.all(),indent=4)
            data = data.decode("unicode_escape").encode("utf8")
            out = open(outfile, "w")
            out.write(data)
            out.close()        

def main():
    for app in get_apps():               
            for model in get_models(app):  
                    print " -- Dumping:", model.__name__
                    outfile = "json_dumps/" + model.__name__ + ".json"                        
                    dumpJson(model, outfile)

但是,如果我删除了try catch语句,则会出现以下错误:\

  -- Dumping: Institution
  Traceback (most recent call last):
  ...(nasty stacktrace)....      
  unity.trip.models.DoesNotExist: USNWRData matching query does not exist.

我的模型定义如下:

class USNWRData(models.Model):
    rank = models.PositiveIntegerField(blank=True, null=False)
    name = models.CharField(max_length=255, blank=True, null=False)
    state = models.CharField(max_length=10, blank=True, null=False)
    public = models.BooleanField(blank=True, null=False)
    type = models.ForeignKey(USNWRType)

class Institution(models.Model):
    name = models.CharField(max_length=200)
    parent_institution = models.ForeignKey('self', blank=True, null=True)
    location = models.ForeignKey(Location, blank=True, null=True,related_name='old_location')
    type = models.ForeignKey(InstitutionType, blank=True, null=False )
    usnwr = models.ForeignKey(USNWRData, blank=True, null=True)
    locationnrm = models.ForeignKey(LocationNrm, blank=False, null=True)

据我了解,此错误的原因是某种数据不匹配或缺少外键。鉴于上述模型,似乎是什么问题?我遇到了一些麻烦,原作者早已不复存在。任何帮助将非常感激!

编辑:完整的堆栈交易是:

 -- Dumping: Institution
 Traceback (most recent call last):
 File "custom_dump.py", line 38, in <module>
    main()
 File "custom_dump.py", line 34, in main
    dumpJson(model, outfile)
 File "custom_dump.py", line 16, in dumpJson
    data = serializers.serialize("json", model.objects.all(),indent=4)
 File "/usr/local/lib64/python2.5/site-packages/django/core/serializers/__init__.py", line 91, in serialize
    s.serialize(queryset, **options)
 File "/usr/local/lib64/python2.5/site-packages/django/core/serializers/base.py", line 48, in serialize
    self.handle_fk_field(obj, field)
 File "/usr/local/lib64/python2.5/site-packages/django/core/serializers/python.py", line 48, in handle_fk_field
    related = getattr(obj, field.name)
 File "/usr/local/lib64/python2.5/site-packages/django/db/models/fields/related.py", line 315, in __get__
    rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
 File "/usr/local/lib64/python2.5/site-packages/django/db/models/query.py", line 349, in get % self.model._meta.object_name)
unity.trip.models.DoesNotExist: USNWRData matching query does not exist.

1 个答案:

答案 0 :(得分:0)

我发现Django灯具对于任何其他目的而言都是无用的,而不是小型数据集(例如,Foo OptionList项目的100条记录)或测试装置。我会通过将MySQL表导出为CSV来解决您的问题。

此外,您可以看一下:

http://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL#MySQL