Show More
@@ -0,0 +1,44 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | from __future__ import unicode_literals | |||
|
3 | ||||
|
4 | from django.db import models, migrations | |||
|
5 | ||||
|
6 | ||||
|
7 | class Migration(migrations.Migration): | |||
|
8 | ||||
|
9 | def clean_duplicate_tags(apps, schema_editor): | |||
|
10 | Tag = apps.get_model('boards', 'Tag') | |||
|
11 | for tag in Tag.objects.all(): | |||
|
12 | tags_with_same_name = Tag.objects.filter(name=tag.name).all() | |||
|
13 | if len(tags_with_same_name) > 1: | |||
|
14 | for tag_duplicate in tags_with_same_name[1:]: | |||
|
15 | threads = tag_duplicate.thread_set.all() | |||
|
16 | for thread in threads: | |||
|
17 | thread.tags.add(tag) | |||
|
18 | tag_duplicate.delete() | |||
|
19 | ||||
|
20 | dependencies = [ | |||
|
21 | ('boards', '0009_post_thread'), | |||
|
22 | ] | |||
|
23 | ||||
|
24 | operations = [ | |||
|
25 | migrations.AlterField( | |||
|
26 | model_name='post', | |||
|
27 | name='thread', | |||
|
28 | field=models.ForeignKey(to='boards.Thread', related_name='pt+'), | |||
|
29 | preserve_default=True, | |||
|
30 | ), | |||
|
31 | migrations.RunPython(clean_duplicate_tags), | |||
|
32 | migrations.AlterField( | |||
|
33 | model_name='tag', | |||
|
34 | name='name', | |||
|
35 | field=models.CharField(db_index=True, unique=True, max_length=100), | |||
|
36 | preserve_default=True, | |||
|
37 | ), | |||
|
38 | migrations.AlterField( | |||
|
39 | model_name='tag', | |||
|
40 | name='required', | |||
|
41 | field=models.BooleanField(db_index=True, default=False), | |||
|
42 | preserve_default=True, | |||
|
43 | ), | |||
|
44 | ] |
@@ -34,8 +34,8 b' class Tag(models.Model, Viewable):' | |||||
34 | app_label = 'boards' |
|
34 | app_label = 'boards' | |
35 | ordering = ('name',) |
|
35 | ordering = ('name',) | |
36 |
|
36 | |||
37 | name = models.CharField(max_length=100, db_index=True) |
|
37 | name = models.CharField(max_length=100, db_index=True, unique=True) | |
38 | required = models.BooleanField(default=False) |
|
38 | required = models.BooleanField(default=False, db_index=True) | |
39 |
|
39 | |||
40 | def __str__(self): |
|
40 | def __str__(self): | |
41 | return self.name |
|
41 | return self.name |
General Comments 0
You need to be logged in to leave comments.
Login now