Python 2.7.2 Ubuntu + Django 1.4; “.objects.get(foo = unicodestr)”时的deepcopy递归错误

时间:2012-06-24 21:25:17

标签: python django django-models

我在一个明显简单的任务上遇到了一个奇怪的错误。

我想使用unicode字符串和以下模型查询sqlite数据库:

from django.db import models

class Uebersetzung(models.Model):
    artikel = models.CharField(max_length=100)
    deutsch = models.CharField(max_length=100)
    turk    = models.CharField(max_length=100)
    added   = models.DateTimeField(default=datetime.now)

查询发生在“独立脚本”中我只使用django的模型和apis,我的查询看起来像这样:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'turk.settings'
from turk.parse.models import *
...

   # each is a tuple of eg. (u'das', u'Haus', u'bina')
   try:
        Uebersetzung.objects.get(artikel=each[0],deutsch=each[1],turk=each[2])
    except Uebersetzung.DoesNotExist:
        tr =  Uebersetzung(artikel=each[0],deutsch=each[1],turk=each[2])
        tr.save()
        print "Adding \'%s %s -> %s\' to Uebersetzung" % (each[0],each[1],each[2]) 

我得到这样的错误:

 File "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)

任何想法?

1 个答案:

答案 0 :(得分:0)

解: 由于我使用BeautifulSoup,我必须将其转换为unicode:

try:
    Uebersetzung.objects.get(artikel=unicode(each[0]),deutsch=unicode(each[1]),turk=unicode(each[2]))

请参阅:python - Problem storing Unicode character to MySQL with Django