views:

244

answers:

3

I'm using Django 1.2 trunk with South 0.7 and an AutoOneToOneField copied from django-annoying. South complained that the field does not have rules defined and the new version of South no longer has an automatic field type parser. So I read the South documentation and wrote the following definition (basically an exact copy of the OneToOneField rules):

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

Now South raises the following error when I do a schemamigration.

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

Is this related to a recent change in Django 1.2 trunk? How do I fix this?

I use this field as follows:

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

For reference the field code from 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))
A: 

Solved the problem by removing the rules and adding the following method to 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)
konrad
+1  A: 

Try to change this line

(AutoOneToOneField),

To this:

(AutoOneToOneField,),

A tuple declared like you did, is not iterable.

Béres Botond
Hmmm, I completely missed this stupid mistake... Thanks!
konrad
A: 

Your rule have simple python related problem.. In tuple, you must add comma if only single item inside.

So change (AutoOneToOneField), to (AutoOneToOneField,),

But to be honest, i didn't know that I can use method inside field instead of rules. I will apply your patch and submit to django-annoying repository.

Great to hear, thanks!
konrad