A migration like this needs to be done in a few distinct migrations.
Here is a nice example from the south tutorial:

    http://south.aeracode.org/docs/tutorial/part3.html#data-migrations

For your example you will probably need to perform these distinct
actions:
  - add tagging_tags
  - migrate data to tagging_tags
  - rename tagging_tags to tags.

1) Add TagField to Tour model and create schemamigration.

    class tour(models.Model):
        # ...
        tagging_tags = TagField() # will rename to tags later

    ./manage.py schemamigration tour --auto

2) Migrate from Tag to TagField.

    ./manage.py datamigration tour switch_from_tags_to_tagging_tags

    # 000x_switch_from_tags_to_tagging_tags.py
    def forwards(self, orm):
        for tour in orm.Tour.objects.all():
            old_tags = ",".join([tag.name for tag in tour.tags.all()])
            tour.tagging_tags = old_tags
            tour.save()

3) Rename the TagField column to tags.
    # delete the Tag class and tweak Tour class
    class Tour(models.model):
        # ...
        tags = TagField() # was tagging_tags

    ./manage.py schemamigration tour --auto

    # 000x_auto__del_tag[...].py
    def forwards(self, orm):
        # ... replace the del_column tagging_tags and add_column tags
w/
        # Renaming field 'Tour.tagging_tags' to 'Tour.tags'
        db.rename_column('tours_tour', 'tagging_tags', 'tags')

Best,
Ian

On Apr 11, 5:06 am, Andrey Torba <andreyto...@gmail.com> wrote:
> Hi all,
> --------
> There are simple model and tag:
> --------
>     class Tag(models.Model):
>         name = models.CharField(max_length=50)
>
>     class Tour(models.Model):
>         name = models.CharField(max_length=50)
>         tags = models.ManyToManyField('Tag',
>                                       related_name='tours',)
>
> --------
> I want refactor data base to use django-tagging.
> --------
>     class Tour(models.Model):
>         name = models.CharField(max_length=50)
>         tags = models.ManyToManyField('Tag',
>                                       related_name='tours',)
>
>     import tagging
>     tagging.register(Tour)
>
> --------
> First i do datamigration:
> --------
>     ./manage.py datamigration --freeze=tagging tour migrate_to_tagging
>
> --------
> Here i shold implement 'forward' and 'backward' methods.
> How to do it? My wrong variant:
> --------
>     class Migration(DataMigration):
>
>         def forwards(self, orm):
>             for tag in orm.Tag.objects.all():
>                 for tour in tag.tours.all():
>                     orm['tagging.Tag'].objects.add_tag(tour, tag.name)
>
>         def backwards(self, orm):
>             for tag in orm['tagging.Tag'].objects.all():
>                 for tour in tag.tours.all():
>                     tour.tags.add(tag.name)
>
> --------
> This is wrong:
> --------
>     orm['tagging.Tag'].objects.add_tag(tour, tag.name)
>
> AttributeError: 'Manager' object has no attribute 'add_tag'
>
> --------
> How to migrate data in right way
>
> --
> Regards, Andrey

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to