# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations class Migration(migrations.Migration): def clean_duplicate_tags(apps, schema_editor): Tag = apps.get_model('boards', 'Tag') for tag in Tag.objects.all(): tags_with_same_name = Tag.objects.filter(name=tag.name).all() if len(tags_with_same_name) > 1: for tag_duplicate in tags_with_same_name[1:]: threads = tag_duplicate.thread_set.all() for thread in threads: thread.tags.add(tag) tag_duplicate.delete() dependencies = [ ('boards', '0009_post_thread'), ] operations = [ migrations.AlterField( model_name='post', name='thread', field=models.ForeignKey(on_delete=models.CASCADE, to='boards.Thread', related_name='pt+'), preserve_default=True, ), migrations.RunPython(clean_duplicate_tags), migrations.AlterField( model_name='tag', name='name', field=models.CharField(db_index=True, unique=True, max_length=100), preserve_default=True, ), migrations.AlterField( model_name='tag', name='required', field=models.BooleanField(db_index=True, default=False), preserve_default=True, ), ]