Django 1.2 + South 0.7 + django-annoying的AutoOneToOneField导致TypeError:'LegacyConnection'对象不可迭代

时间:2010-05-06 12:52:45

标签: python django django-models django-south

我正在使用Django 1.2 trunk和South 0.7以及从django-annoying复制的AutoOneToOneField。 South抱怨说该字段没有定义规则,South的新版本不再具有自动字段类型解析器。所以我阅读了South文档并编写了以下定义(基本上是OneToOneField规则的精确副本):

rules = [
  (
    (AutoOneToOneField),
    [],
    {
        "to": ["rel.to", {}],
        "to_field": ["rel.field_name", {"default_attr": "rel.to._meta.pk.name"}],
        "related_name": ["rel.related_name", {"default": None}],
        "db_index": ["db_index", {"default": True}],
    },
  )
]
from south.modelsinspector import add_introspection_rules
add_introspection_rules(rules, ["^myapp"]) 

现在,当我执行模式移植时,South会引发以下错误。

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "django/core/management/base.py", line 223, in execute
    output = self.handle(*args, **options)
  File "South-0.7-py2.6.egg/south/management/commands/schemamigration.py", line 92, in handle
    (k, v) for k, v in freezer.freeze_apps([migrations.app_label()]).items()
  File "South-0.7-py2.6.egg/south/creator/freezer.py", line 33, in freeze_apps
    model_defs[model_key(model)] = prep_for_freeze(model)
  File "South-0.7-py2.6.egg/south/creator/freezer.py", line 65, in prep_for_freeze
    fields = modelsinspector.get_model_fields(model, m2m=True)
  File "South-0.7-py2.6.egg/south/modelsinspector.py", line 322, in get_model_fields
    args, kwargs = introspector(field)
  File "South-0.7-py2.6.egg/south/modelsinspector.py", line 271, in introspector
    arg_defs, kwarg_defs = matching_details(field)
  File "South-0.7-py2.6.egg/south/modelsinspector.py", line 187, in matching_details
    if any([isinstance(field, x) for x in classes]):
TypeError: 'LegacyConnection' object is not iterable

这与Django 1.2 trunk的最近更改有关吗?我该如何解决这个问题?

我按如下方式使用此字段:

class Bar(models.Model):
    foo = AutoOneToOneField("foo.Foo", primary_key=True, related_name="bar")

参考django-tagging的字段代码:

class AutoSingleRelatedObjectDescriptor(SingleRelatedObjectDescriptor):
    def __get__(self, instance, instance_type=None):
        try:
            return super(AutoSingleRelatedObjectDescriptor, self).__get__(instance, instance_type)
        except self.related.model.DoesNotExist:
            obj = self.related.model(**{self.related.field.name: instance})
            obj.save()
            return obj

class AutoOneToOneField(OneToOneField):
    def contribute_to_related_class(self, cls, related):
        setattr(cls, related.get_accessor_name(), AutoSingleRelatedObjectDescriptor(related))

3 个答案:

答案 0 :(得分:5)

尝试更改此行

(AutoOneToOneField),

对此:

(AutoOneToOneField,),

像你一样声明的元组是不可迭代的。

答案 1 :(得分:3)

通过删除规则并将以下方法添加到AutoOneToOneField来解决问题:

def south_field_triple(self):
    "Returns a suitable description of this field for South."
    from south.modelsinspector import introspector
    field_class = OneToOneField.__module__ + "." + OneToOneField.__name__
    args, kwargs = introspector(self)
    return (field_class, args, kwargs)

答案 2 :(得分:1)

你的规则有简单的python相关问题..在元组中,如果只有单个项目,你必须添加逗号。

因此将(AutoOneToOneField)更改为(AutoOneToOneField),

但说实话,我不知道我可以在字段内使用方法而不是规则。我将应用你的补丁并提交给django-annoying repository。