gpt4 book ai didi

python - Django 1.2 + South 0.7 + django-annoying 的 AutoOneToOneField 导致 TypeError : 'LegacyConnection' object is not iterable

转载 作者:太空狗 更新时间:2023-10-30 02:06:33 25 4
gpt4 key购买 nike

我正在使用带有 South 0.7 的 Django 1.2 主干和从 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 主干最近的变化有关?我该如何解决这个问题?

我按如下方式使用这个字段:

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))

最佳答案

尝试改变这一行

(AutoOneToOneField),

对此:

(AutoOneToOneField,),

像您那样声明的元组是不可迭代的。

关于python - Django 1.2 + South 0.7 + django-annoying 的 AutoOneToOneField 导致 TypeError : 'LegacyConnection' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2781210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com