-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1633] Migration removing unused keywords
Keywords that aren't used in any project are deleted.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
|
||
def remove_unused_keys(apps, schema_editor): | ||
Keyword = apps.get_model('rsr', 'Keyword') | ||
Project = apps.get_model('rsr', 'Project') | ||
|
||
for keyword in Keyword.objects.all(): | ||
projects = Project.objects.filter(keywords__exact=keyword) | ||
if not projects: | ||
print "Deleting keyword {}".format(keyword.label) | ||
keyword.delete() | ||
|
||
print "Keyword distribution after migration:" | ||
for keyword in Keyword.objects.all(): | ||
projects = Project.objects.filter(keywords__exact=keyword) | ||
print keyword.label, projects.count() | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('rsr', '0024_auto_20150819_1149'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
remove_unused_keys | ||
), | ||
] |