2010-06-16 95 views
4

从转储数据安装灯具时出现奇怪的错误。我正在使用psycopg2和django1.1.1django:从postgres数据库导入数据时无法修改错误

silver:probsbox oleg$ python manage.py loaddata /Users/oleg/probs.json 
Installing json fixture '/Users/oleg/probs' from '/Users/oleg/probs'. 
Problem installing fixture '/Users/oleg/probs.json': Traceback (most recent call last): 
    File "/opt/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py", line 153, in handle 
    obj.save() 
    File "/opt/local/lib/python2.5/site-packages/django/core/serializers/base.py", line 163, in save 
    models.Model.save_base(self.object, raw=True) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/base.py", line 495, in save_base 
    result = manager._insert(values, return_id=update_pk) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/manager.py", line 177, in _insert 
    return insert_query(self.model, values, **kwargs) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/query.py", line 1087, in insert_query 
    return query.execute_sql(return_id) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/sql/subqueries.py", line 320, in execute_sql 
    cursor = super(InsertQuery, self).execute_sql(None) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql 
    cursor.execute(sql, params) 
    File "/opt/local/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute 
    return self.cursor.execute(sql, params) 
ProgrammingError: can't adapt 

首先我检查了互联网上的类似问题。这一个似乎非常有关:http://code.djangoproject.com/ticket/5996,因为我的数据有许多非ASCII符号

但实际上我已经检查了我的Django的安装和它的确定有

难道你的建议是什么错误

= ===

根据第一个答案建议的添加打印语句后继续调查。日志看起来是这样的:从最后评论

silver:probsbox oleg$ python manage.py loaddata /Users/oleg/probs.json 
Installing json fixture '/Users/oleg/probs' from '/Users/oleg/probs'. 
<DeserializedObject: Novice> 
<DeserializedObject: Junior> 
<DeserializedObject: Chess enthusiast> 
<DeserializedObject: Experienced player > 
<DeserializedObject: Smart player> 
Problem installing fixture '/Users/oleg/probs.json': Traceback (most recent call last): 
    File "/opt/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py", line 153, in handle 
    print obj 
    File "/opt/local/lib/python2.5/site-packages/django/core/serializers/base.py", line 155, in __repr__ 
    return "<DeserializedObject: %s>" % smart_str(self.object) 
    File "/opt/local/lib/python2.5/site-packages/django/utils/encoding.py", line 107, in smart_str 
    return str(s) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/base.py", line 335, in __str__ 
    return force_unicode(self).encode('utf-8') 
    File "/opt/local/lib/python2.5/site-packages/django/utils/encoding.py", line 71, in force_unicode 
    s = unicode(s) 
    File "/Users/oleg/Sites/probsbox/registration/models.py", line 58, in __unicode__ 
    return u"%s's profile" %(self.user.username) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/fields/related.py", line 257, in __get__ 
    rel_obj = QuerySet(self.field.rel.to).get(**params) 
    File "/opt/local/lib/python2.5/site-packages/django/db/models/query.py", line 305, in get 
    % self.model._meta.object_name) 
DoesNotExist: User matching query does not exist. 

silver:probsbox oleg$ 

错误

<DeserializedObject: qwert2000's profile> 

问题安装夹具 '/Users/oleg/probs.json':回溯(最近通话最后一个): 文件“/ opt/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py“,第154行,在句柄 obj.save() 文件”/ opt/local/lib/python2。 5/site-packages/django/core/serializers/base.py“,第163行,保存为 models.Model.save_base(self.object,raw = True) File”/ op t/local/lib/python2.5/site-packages/django/db/models/base.py“,第495行,在save_base result = manager._insert(values,return_id = update_pk) ”/ opt/local /lib/python2.5/site-packages/django/db/models/manager.py“,第177行,在_insert中 return insert_query(self.model,values,** kwargs) ”/ opt/local/lib /python2.5/site-packages/django/db/models/query.py“,第1087行,在insert_query中 return query.execute_sql(return_id) 文件”/opt/local/lib/python2.5/site-packages /django/db/models/sql/subqueries.py“,第320行,在execute_sql中 cursor = super(InsertQuery,self).execute_sql(None) 文件”/opt/local/lib/python2.5/site-packages /django/db/models/sql/query.py“,第2369行,在execute_sql中 cursor.execute(sql,par ams) 文件“/opt/local/lib/python2.5/site-packages/django/db/backends/util.py”,第19行,执行 return self.cursor.execute(sql,params) ProgrammingError :不能适应

回答

0

好吧,我最终从我的数据库复制转储,并在本地恢复它没有巨蟒...

5

can't adapt错误是由psycopg2引发的,当它接收到一个数据类型时,它不知道如何转换为SQL语句的值。例如,如果您不小心传递了一个列表,比如说,应该是一个整数的值,psycopg2将会引发这个错误。

faq.txt文件附带psycopg2的源代码分发这样解释道:

Why does !cursor.execute() raise the exception can't adapt?

Psycopg converts Python objects in a SQL string representation by looking at the object class. The exception is raised when you are trying to pass as query parameter an object for which there is no adapter registered for its class. See :ref: adapting-new-types for informations.

可能是你最好先通在发现错误的值是在充分详细模式运行loaddata:蟒蛇管理的.py loaddata --verbosity = 2 /Users/oleg/probs.json

嗯,我希望loaddata冗长会工作,我也不会承认,我从来没有发现调试适应的一种优雅的方式Django的loaddata错误。在过去,我已经在django的loaddata函数中插入了打印语句,以便在出现错误时可以看到值被反序列化。我编辑了django/core/management/loaddata.py。看obj.save()handle()功能。我希望这表白激励别人分享:-)

一个更好的解决方案
+0

MMMM ....所以,我该怎么办?我只需要将数据导入数据库...也许有另一种方式做到这一点? – 2010-06-16 18:10:39

+0

如果您采用与我相同的低级方法,请在系统上打开django的副本,并在loaddata.py模块的obj.save()之前添加一条打印语句,以查看您要传出的错误数据的系统。问题将成为堆栈跟踪之前的最后一条打印语句。 – 2010-06-16 19:41:32

+0

是的。看我最近的编辑。但我不明白这个问题是怎么出现的...为什么 DoesNotExist:用户匹配查询不存在。 – 2010-06-16 23:38:57