Migration for book count fix.
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 11 May 2010 11:12:06 +0000 (13:12 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 11 May 2010 11:12:06 +0000 (13:12 +0200)
apps/catalogue/migrations/0008_fix_shelf_book_count.py [new file with mode: 0644]

diff --git a/apps/catalogue/migrations/0008_fix_shelf_book_count.py b/apps/catalogue/migrations/0008_fix_shelf_book_count.py
new file mode 100644 (file)
index 0000000..b4ee915
--- /dev/null
@@ -0,0 +1,18 @@
+# -*- coding: utf-8 -*-
+
+from south.db import db
+from django.db import models
+from catalogue.models import Tag, Book
+
+class Migration:
+    
+    def forwards(self):
+        "Write your forwards migration here"
+        for tag in Tag.objects.filter(user__isnull=False):
+            books = Tag.intermediary_table_model.objects.get_intersection_by_model(Book, [tag])
+            tag.book_count = len(books)
+            tag.save()
+    
+    def backwards(self, orm):
+        "Write your backwards migration here"
+        pass