X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/a9693d28c16486fced04ba9cdb66a78781f30832..b4674b1b0f73651289787c379e1bc726d38c97a9:/migrations/001_add_book_count_to_shelf.py?ds=sidebyside diff --git a/migrations/001_add_book_count_to_shelf.py b/migrations/001_add_book_count_to_shelf.py new file mode 100644 index 000000000..4af55a172 --- /dev/null +++ b/migrations/001_add_book_count_to_shelf.py @@ -0,0 +1,16 @@ +from django.core.management import setup_environ +import settings + +setup_environ(settings) + +from catalogue.models import Tag, Book +from django.db import connection + +query = 'ALTER TABLE catalogue_tag ADD COLUMN book_count integer NOT NULL DEFAULT 0' + +cursor = connection.cursor() +cursor.execute(query) + +for shelf in Tag.objects.filter(category='set'): + shelf.book_count = len(Book.tagged.with_all(shelf)) + shelf.save()