From b4674b1b0f73651289787c379e1bc726d38c97a9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20St=C4=99pniowski?= Date: Thu, 25 Sep 2008 17:15:58 +0200 Subject: [PATCH] Added book_count field to Tag model. Added corresponding migration. --- apps/catalogue/models.py | 1 + migrations/001_add_book_count_to_shelf.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 migrations/001_add_book_count_to_shelf.py diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 133440b3b..ec3c32ef6 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -43,6 +43,7 @@ class Tag(TagBase): main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page')) user = models.ForeignKey(User, blank=True, null=True) + book_count = models.IntegerField(_('book count')) def has_description(self): return len(self.description) > 0 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() -- 2.20.1