# HG changeset patch # User neko259 # Date 2015-02-08 13:05:02 # Node ID c3e4aa3ea4a36f28ee5cb3d5dcb9a0cbe62263a1 # Parent c170e0ea51810362e60f97432f6a68dc485c1c01 Made tag name field unique. Added index to "required" field for tag diff --git a/boards/migrations/0010_auto_20150208_1451.py b/boards/migrations/0010_auto_20150208_1451.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0010_auto_20150208_1451.py @@ -0,0 +1,44 @@ +# -*- 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(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, + ), + ] diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -34,8 +34,8 @@ class Tag(models.Model, Viewable): app_label = 'boards' ordering = ('name',) - name = models.CharField(max_length=100, db_index=True) - required = models.BooleanField(default=False) + name = models.CharField(max_length=100, db_index=True, unique=True) + required = models.BooleanField(default=False, db_index=True) def __str__(self): return self.name